2009-07-16 39 views
1
Dim customers As List(Of Customer) = New List(Of Customer) 
    For Each mbi In fordContracts 
     customers.Add(mbi.Customer) 
    Next 

是否有可能爲客戶查詢fordContracts?它是一個IList(mbi),每個mbi對象都有一個到Customer對象的EntityRef。我只是想知道是否有更好的方法來實現這個使用Linq。我可以通過linq而不是For Each來實現嗎?

謝謝。

回答

3

如果要添加到現有的列表(其中可能有一些元素的話):

customers.AddRange(From mbi In fordContracts Select mbi.Customer) 

如果你想獲得一個全新的列表:

customers = (From mbi In fordContracts Select mbi.Customer).ToList() 
+0

美麗!這正是我正在尋找的。好樣的! – Hcabnettek 2009-07-16 05:27:18

相關問題