2013-01-31 54 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)); 
相關問題