2011-01-09 20 views
0

にジェネリックリストから特定の文字列フィールドを取得、のは次のように私の好きなクラスを持っていると仮定してみましょう。..は、C#で配列

public class anItem 
    { 
     public string name { get; set; } 
     public string surname { get; set; } 
    } 

と私は次のように、そのオブジェクトにジェネリックリストを使用します。

List<anItem> listof = new List<anItem>(); 
listof.Add(new anItem { name = "name 1", surname = "surname 1" }); 
listof.Add(new anItem { name = "name 2", surname = "surname 2" }); 
listof.Add(new anItem { name = "name 3", surname = "surname 3" }); 
listof.Add(new anItem { name = "name 4", surname = "surname 4" }); 

ことが可能listofジェネリックリストから文字列配列にすべての姓のを取るのですか?

string[] takenSurnames = // take just surnames from listof 

はい私はループのforeachのまたはを持つことを得ることができます。しかし、ラムダ式があるのか​​、それとも短いものがあるのだろうか?事前に

おかげ..

答えて

4
listof.Select(c => c.surename).ToArray(); 
+0

おかげでイタリア... –