c# – Using value from related table in LINQ where clause – Education Career Blog

I was expecting the following LINQ query to retrieve all contacts with the specified phone number but instead it returns all contacts that don’t have a phone number at all.

var query = from contact in dc.Contacts
            where contact.Phones.All(phone => phone.PhoneNumber == "5558675309")
            select contact;

What am I doing wrong here?

,

I should have been using the Any extension method, not All.

The following code works just fine:

var query = from contact in dc.Contacts            
            where contact.Phones.Any(p => p.PhoneNumber == "5558675309")            
            select contact;

Leave a Comment