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

I am developing an ASP.NET MVC 4 application and I am trying to run this Lambda expression in Entity Framework 5.

 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)); 

I get this error:

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

I know that the last where clause is wrong, but I don’t know how to fix it.

+4
source share
2 answers

At the end of .Any , you can add .Any at the end instead of .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)); 
+8
source

Use Where ( Any ) in the last statement to select customers who have at least one product that meets your conditions:

 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)); 
+3
source

All Articles