gpt4 book ai didi

c# - 我可以在通用列表中使用 string.join

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:14 25 4
gpt4 key购买 nike

有没有办法采用通用列表并将某个属性加入逗号分隔值?

public class Color
{
public int Id {get; set;}
public string Name{get; set;}
}

List<Color> list = new List<Color>();
Color c1 = new Color() { Id = 1, Name = "red" }
Color c2 = new Color() { Id = 2, Name = "blue" }

我可以做类似的事情吗

list.Join(

得到

"red, blue";

最佳答案

不,你不能。此方法定义为 string.Join(String, String[])。有一种方法可以完成您想要做的事情:

string.Join(",", list.Select(c=>c.Name).ToArray());

关于c# - 我可以在通用列表中使用 string.join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com