无法将类型System.Collections.Generic.IEnumerable 隐式转换为bool
我正在开发一个ASP.NET MVC 4应用程序,我正在尝试在Entity Framework 5中运行这个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));
我收到此错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'
我知道最后一个where子句是错误的,但我不知道如何纠正它。
您最后可能需要另一个.Any
而不是.Any
子句中的.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));