2009-07-16 71 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

相关问题