[英]How to use a Func in an expression with Linq to Entity Framework?
我正在尝试将linq写入实体扩展方法,该方法使用Func选择属性Id并将其与ID列表进行比较。
类
public class A
{
public int AId { get; set; }
}
public class B
{
public int BId { get; set; }
}
扩展方法
public static IQueryable<T> WithId<T>(this IQueryable<T> entities,
Func<T, int> selector, IList<int> ids)
{
Expression<Func<T, bool>> expression = x => ids.Contains(selector(x));
return entities.Where(expression); // error here (when evaluated)
}
呼叫方法
var ids = new List<int> { 1, 2, 3 };
DbContext.EntityAs.WithId(e => e.AId, ids);
DbContext.EntityBs.WithId(e => e.BId, ids);
我遇到的问题是它试图调用Entity Framework中不允许的功能。
如何使用属性选择器(Func)来评估查询?
您必须传递Expression<Func<T, int>>
而不是Func<T, int>
并自己构建完整的表达式。 这样就可以了:
public static IQueryable<T> WithId<T>(this IQueryable<T> entities,
Expression<Func<T, int>> propertySelector, ICollection<int> ids)
{
var property =
(PropertyInfo)((MemberExpression)propertySelector.Body).Member;
ParameterExpression parameter = Expression.Parameter(typeof(T));
var expression = Expression.Lambda<Func<T, bool>>(
Expression.Call(
Expression.Constant(ids),
typeof(ICollection<int>).GetMethod("Contains"),
Expression.Property(parameter, property)),
parameter);
return entities.Where(expression);
}
当您在使用O / RM时尝试将代码保持干燥时,通常需要摆弄表达式树。 这是另一个有趣的例子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.