8
votes

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.

1
Where is categoryIds and sections coming from?tbddeveloper
They come from my code. They end up being comma separated lists of Ids. I noticed the problem is definitely because of the part of the where clause that has the null check and the contains.Matthew Peterson
Can you post the Product class as well?Glorfindel
The product class is just a basic class with some product properties auto-generated by entity framework. The main thing causing the error is the fact that I am doing a null check for each Contains line of the where clause. All of my other GetList functions work fine because they don't use Contains. I need to find a way so passing those lists to my function is optional and this is normally what I do.Matthew Peterson

1 Answers

4
votes

If it doesn't like the null check and you need it to be optional, you can do this:

List<int> productCategoryIdsTemp = new List<int>();
if (productCategoryIds != null) {
    productCategoryIdsTemp.AddRange(productCategoryIds.Where(id => id != null).Select(id => id.Value));
}
if (sections = null) { 
    sections = new List<string>();
}

And then in your Linq query use this:

(productCategoryIdsTemp.Count == 0 || productCategoryIdsTemp.Contains(obj.ProductCategoryId)) &&
(sections.Count == 0 || sections.Contains(obj.section)) &&

If your productCategoryIds wasn't an IEnumerable of nullable ints you could do the same as for sections. (Don't really understand how this needs to be supported instead of a list of int)