我能用linq代替For Each吗?

时间:2009-07-16 05:18:26

标签: linq linq-to-sql

 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。

感谢。

1 个答案:

答案 0 :(得分:3)

如果您要添加到现有列表(可能已经有一些元素):

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

如果你想获得一个全新的清单:

customers = (From mbi In fordContracts Select mbi.Customer).ToList()
相关问题