LINQ Where is the statement in the collection

I have a Customer object that has a ContactNumbers collection. 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..... ? 
+4
source share
1 answer

Try to execute

 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 elements in the collection matches the predicate. In this case, y.Number = 123

+8
source

Source: https://habr.com/ru/post/1403936/


All Articles