【发布时间】:2017-11-27 09:11:17
【问题描述】:
背景:我想使用 System.Linq.Dynamic 库将 In/Contains 查询发布到 MS SQL
请注意,我正在尝试在通用函数中使用 System.Linq.Dynamic,以便我可以将客户过滤器应用于任何具有 CustomerId(integer) 属性的类。 CustomerId 属性也可以为空
所有 SO 帖子都将我重定向到 this solution。在实现相同的功能后,我不断收到此异常:“类型'System.Linq.Enumerable'上的通用方法'Contains'与提供的类型参数和参数兼容。如果方法是非通用的,则不应提供类型参数。 "
现在这就是我的代码的样子:
public static IQueryable<T> ApplyCustomerFilter<T>(this IQueryable<T> collection, int[] customerIds)
{
return collection.Where("@0.Contains(outerIt.CustomerId)", customerIds);
}
public class MyUser : IdentityUser
{
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
public int? CustomerId { get; set; }
[ForeignKey(nameof(CustomerId))]
public virtual Customer Customer { get; set; }
}
你能指导我哪里出错了吗?
【问题讨论】:
-
我猜这只有在
T is int时才有效,使得泛型函数相当非泛型? -
这不是真的。它适用于任何具有客户 ID 的类。
标签: c# linq entity-framework-6