2012-03-28 81 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