我有一个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..... ?
Run Code Online (Sandbox Code Playgroud)
请尝试以下方法
customers = customers.Where(Function (x) x.ContactNumbers.Any(Function (y) y.Number = "123"))
Run Code Online (Sandbox Code Playgroud)
这里的诀窍是Any功能.True如果集合中的任何项与谓词匹配,则返回此选项.在这种情况下y.Number = 123