我有很多像这样的查询,但我不明白为什么这个错误。当我进行空检查然后使用Contains时,似乎它与我的where子句的某些部分有关。实体框架Linq查询列表 - 使用时出错包含:仅支持基本类型,枚举类型和实体类型
我得到的错误是:
无法比拟的类型元素“System.Collections.Generic.IEnumerable`1 [System.Nullable`1 [System.Int32,mscorlib程序,版本= 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'。只支持原始类型,枚举类型和实体类型。
以及它抛出代码:
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;
}
}
这些都是使得它的错误线。我相信,它不会像空校验:
(productCategoryIds == null || productCategoryIds.Contains(obj.productCategoryIds)) &&
(sections == null || sections.Contains(obj.Section))
这里是我的方法调用(部分没有被通过):
List<int?> categoryIds = new List<Int?>;
varList = ProductsDAL.GetProducts(productDepartmentId: productproductDeparmentId,
productCategoryId: productCategoryId,
productCategoryIds: categoryIds);
我也试图传递的List键入int。
categoryIds和sections从哪里来? – Hammerstein
他们来自我的代码。它们最终成为逗号分隔的ID列表。我注意到这个问题肯定是由于where子句中有空检查和包含的部分。 –
可能重复[无法比较'System.Collections.Generic.ICollection'类型的元素1只支持原始类型,枚举类型和实体类型](http://stackoverflow.com/questions/23937112/cannot-compare-元素类型 - 系统集合 - generic-icollection1-only-p) –