LINQ Where语句集合

时间:2012-03-28 15:16:01

标签: vb.net linq

我有一个Customer对象,其中包含ContactNumbers的集合。 是否可以通过LINQ获取客户列表,其中一个联系号码='123'?

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..... ?

1 个答案:

答案 0 :(得分:8)

尝试以下

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

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