2013-05-03 56 views
0

的名單我有一個IList --->自IList選擇所有字段,包含對象

IList<Test> list; 

列表中包含了一流的試驗對象,如下:

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

其中類測試是--->

Class Test 
{ 
    int id; 
    string name; 
} 

現在我想要select所有名稱字段我被卡在(缺乏對LINQ C#的知識)

回答

2
IList<string> nameList = YourCollection.Select(item=> item.name).ToList(); 
+0

其工作thnx! – 2013-05-03 07:43:59

3

您可以使用LINQ Select --->

IList<string> nameList = ??? 

(列表中的所有元素的):

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

增加了「;」在尾部。 – David 2013-05-03 07:39:38

相關問題