2013-01-31 52 views
4

我开发一个ASP.NET MVC 4应用程序,我想在实体框架5无法隐式转换类型System.Collections.Generic.IEnumerable <>为bool

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)); 
运行这个Lambda表达式

我得到这个错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<ITKaranDomain.GNL_CustomerLaptopProduct>' to 'bool'

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

+0

正如Andrew在他的回答中指出的那样,您需要使用'Any'作为'Where where will return an'IEnumerable ''而Any'的谓词需要一个'bool'返回函数ñ。 –

回答

8

您可能希望另一.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)); 
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)); 
相关问题