I have many queries just like this one, but I can't figure out why this one is erroring. It seems like it has something to do with the parts of my where clause when I am doing the null check and then using Contains.
The error I am getting is:
Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1[[System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only primitive types, enumeration types and entity types are supported.
And the code where it is thrown:
public static IEnumerable<Product> GetProducts(int? productDepartmentId = null, int? productCategoryId = null, IEnumerable<int?> productCategoryIds = null, IEnumerable<string> sections = null)
{
using (var context = new AppContext())
{
var retList = (from obj in context.Products
where (productDepartmentId == null || obj.ProductDepartmentId == productDepartmentId) &&
(productCategoryId == null || obj.ProductCategoryId == productCategoryId) &&
(productCategoryIds == null || productCategoryIds.Contains(obj.ProductCategoryId)) &&
(sections == null || sections.Contains(obj.sections))
select obj).ToList();
return retList;
}
}
These are the lines that are making it error. I believe it doesn't like the null check:
(productCategoryIds == null || productCategoryIds.Contains(obj.productCategoryIds)) &&
(sections == null || sections.Contains(obj.Section))
Here is my call to the method (sections isn't being passed):
List<int?> categoryIds = new List<Int?>;
varList = ProductsDAL.GetProducts(productDepartmentId: productproductDeparmentId,
productCategoryId: productCategoryId,
productCategoryIds: categoryIds);
I have also tried passing in a List of type int.