2012-03-28 83 views
4

我有一个Customer对象,其中包含ContactNumbers的集合。 LINQ有可能获得其中一个联系号码='123'的客户列表吗?LINQ集合中的声明

Public Class Customer 
    Public Overridable Property ContactNumbers As List(Of ContactNumber) 

End Class 

Public Class ContactNumber 

    <Required()> 
    Public Property ID As Integer 

    <Required()> 
    <StringLength(20)> 
    Public Property Number As String 

    Public Overridable Property Type As ContactNumberType 

    Public Property Primary As Boolean 

End Class 


Dim findnumber as String = '123' 
Dim customers = db.customers.tolist 

customers = customers.Where..... ? 

回答

8

请尝试以下

customers = customers.Where(Function (x) x.ContactNumbers.Any(Function (y) y.Number = "123")) 

的伎俩在这里是Any功能。如果集合中的任何项目与谓词匹配,则这将返回True。在这种情况下y.Number = 123

+1

该死的,我很近! 谢谢! – Tom 2012-03-28 15:31:25