使用LINQ where子句中相关表的值

时间:2021-11-10 11:48:07

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.

我期待以下LINQ查询检索具有指定电话号码的所有联系人,但它返回所有没有电话号码的联系人。

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

What am I doing wrong here?

我在这做错了什么?

1 个解决方案

#1


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

我本来应该使用Any扩展方法,而不是All。

The following code works just fine:

以下代码工作得很好:

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

#1


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

我本来应该使用Any扩展方法,而不是All。

The following code works just fine:

以下代码工作得很好:

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