简体   繁体   English

用对象列表中的参数填充列表 c#

[英]fill a list with a parameter in an object list c#

Good morning, I'm looking for a more efficient way to fill a list of any type with an object parameter.早上好,我正在寻找一种更有效的方法来使用对象参数填充任何类型的列表。

Example (my class) :示例(我的班级):

public class Thème
{
    public string Thème_ { get; set; }
    public Liste[] Listes { get; set; }
}

My code that I currently have to do what I want to do:我目前必须做我想做的代码:

        List<string> nomDesThèmesDisponible = new List<string>();

        foreach(Thème thème in Data.Thèmes)
        {
            nomDesThèmesDisponible.Add(thème.Thème_);
        }

I think there's an easier way to do that (with linq?).我认为有一种更简单的方法来做到这一点(使用 linq?)。

Thank you for offering me your solutions感谢您为我提供您的解决方案

You can do你可以做

nomDesThèmesDisponible.AddRange(Data.Thèmes.Select(t => t.Thème_));

or或者

nomDesThèmesDisponible = Data.Thèmes.Select(t => t.Thème_).ToList();

These ways not anymore efficient, just more concise.这些方法不再有效,只是更简洁。

List<string> nomDesThèmesDisponible = Data.Thèmes.Select (t => t.Thème_).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM