早上好,我正在寻找一种更有效的方法来用对象参数填充任何类型的列表。
示例(我的类):
public class Thème
{
public string Thème_ { get; set; }
public Liste[] Listes { get; set; }
}我的代码,我现在必须做我想做的事情:
List<string> nomDesThèmesDisponible = new List<string>();
foreach(Thème thème in Data.Thèmes)
{
nomDesThèmesDisponible.Add(thème.Thème_);
}我认为有一种更简单的方法(使用linq?)。
感谢您为我提供解决方案
发布于 2020-03-21 21:05:25
你可以做到
nomDesThèmesDisponible.AddRange(Data.Thèmes.Select(t => t.Thème_));或
nomDesThèmesDisponible = Data.Thèmes.Select(t => t.Thème_).ToList();这些方法不再有效,只是更简洁。
发布于 2020-03-21 21:07:59
List<string> nomDesThèmesDisponible = Data.Thèmes.Select (t => t.Thème_).ToList();https://stackoverflow.com/questions/60788077
复制相似问题