【问题标题】:select all fields from ilist, contains list of objects从 ilist 中选择所有字段,包含对象列表
【发布时间】:2013-05-03 07:36:13
【问题描述】:

我有一个 IList--->

IList<Test> list;   

该列表包含 Test 类的对象,如下所示:

list.Add(new Test{ id = 1, name = "abc" })
list.Add(new Test{ id = 2, name = "xyz" })
list.Add(new Test{ id = 3, name = "nmo" })

Test 类在哪里 --->

Class Test
{
    int id;
    string name;
}

现在我想选择所有名称字段(列表的所有元素)--->

IList<string> nameList = ???  

我陷入了困境(缺乏关于 LINQ c# 的知识)

【问题讨论】:

    标签: c# linq ienumerable ilist


    【解决方案1】:

    你可以使用 LINQ Select:

     nameList = list.Select(x => x.name).ToList();
    

    【讨论】:

      【解决方案2】:
      IList<string> nameList = YourCollection.Select(item=> item.name).ToList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 2021-11-02
        • 2014-12-19
        • 1970-01-01
        相关资源
        最近更新 更多