I have a Customer object which has a collection of ContactNumbers. Is it possible with LINQ to get a list of Customers where one of the contact numbers = '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..... ?
Try the following
customers = customers.Where(Function (x) x.ContactNumbers.Any(Function (y) y.Number = "123"))
The trick here is the Any
function. This will return True
if any of the items in the collection match the predicate. In this case y.Number = 123
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With