C# 无法隐式转换类型System.Collections.Generic.IEnumerable<&燃气轮机;哄骗

C# 无法隐式转换类型System.Collections.Generic.IEnumerable<&燃气轮机;哄骗,c#,entity-framework,C#,Entity Framework,我正在开发一个ASP.NETMVC4应用程序,并试图在EntityFramework5中运行这个Lambda表达式 var customer = db.GNL_Customer.Where(d => d.GNL_City.FKProvinceID == advancedProvinceID || advancedProvinceID == null) .Where(d => d.FKCityID == advancedCityID || advancedCit

我正在开发一个ASP.NETMVC4应用程序,并试图在EntityFramework5中运行这个Lambda表达式

var customer = db.GNL_Customer.Where(d => d.GNL_City.FKProvinceID == advancedProvinceID || advancedProvinceID == null)
            .Where(d => d.FKCityID == advancedCityID || advancedCityID == null)
            .Where(d => d.FKDepartmentStoreID == advancedDepartmentStoreID || advancedDepartmentStoreID == null)
            .Where(d => d.GNL_CustomerLaptopProduct.Where(r => String.Compare(r.BrandName, brandID) == 0 || brandID == null));
我得到这个错误:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“bool”


我知道最后一个where子句是错误的,但我不知道如何更正。

您可能需要另一个
。任何
而不是
。where
在您的
中。最后的任何
子句:

var customer = db.GNL_Customer.Where(d => d.GNL_City.FKProvinceID == advancedProvinceID || advancedProvinceID == null)
            .Where(d => d.FKCityID == advancedCityID || advancedCityID == null)
            .Where(d => d.FKDepartmentStoreID == advancedDepartmentStoreID || advancedDepartmentStoreID == null)
            .Any(d => d.GNL_CustomerLaptopProduct.Any(r => String.Compare(r.BrandName, brandID) == 0 || brandID == null));
使用上一句中的
Where(Any)
选择至少有一种产品满足您的条件的客户:

var customer = db.GNL_Customer
   .Where(d => d.GNL_City.FKProvinceID == advancedProvinceID || advancedProvinceID == null)
   .Where(d => d.FKCityID == advancedCityID || advancedCityID == null)
   .Where(d => d.FKDepartmentStoreID == advancedDepartmentStoreID || advancedDepartmentStoreID == null)
   .Where(d => brandID == null || d.GNL_CustomerLaptopProduct.Any(r => r.BrandName == brandID));

正如Andrew在他的回答中指出的那样,您需要使用
Any
作为
Where将返回一个
IEnumerable`并且
Any
的谓词需要一个
bool
返回函数。