10

我正在开发一个 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 子句是错误的,但我不知道如何纠正它。

4

2 回答 2

18

您可能希望在最后的子句中使用另一个.Any而不是 a :.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));
于 2013-01-31T13:45:31.560 回答
5

在最后一个语句中使用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));
于 2013-01-31T14:05:14.480 回答