无法隐式转换类型System.Collections.Generic.IEnumerable<>布尔

时间:2013-01-31 13:43:15

标签: c# entity-framework

我正在开发一个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<ITKaranDomain.GNL_CustomerLaptopProduct>' to 'bool'

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

2 个答案:

答案 0 :(得分:10)

您最后可能需要在.Any子句中使用其他.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)
            .Any(d => d.GNL_CustomerLaptopProduct.Any(r => String.Compare(r.BrandName, brandID) == 0 || brandID == null));

答案 1 :(得分:3)

在最后一条语句中使用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));