关于这个主题已经有一些问题(例如实体框架中的表达式.调用? https://stackoverflow.com/questions/10524/expression-invoke-in-entity-framework),但是,我找不到适合我的具体情况的答案。
我想定义一个这样的方法:
public IQueryable<Customer> GetCustomers(Expression<Func<Customer, bool>> condition)
{
return from p in ctx.Customers.AsExpandable()
where condition.Compile()(p)
select p;
}
AsExpandable 方法来自 LinqKit(正如前面提到的线程中建议的那样)。
但是,当我尝试像他一样调用我的方法时:
var customers = GetCustomers(c => c.ID == 1);
它抛出一个 InvalidCastException:
无法将“System.Linq.Expressions.InstanceMethodCallExpressionN”类型的对象强制转换为“System.Linq.Expressions.LambdaExpression”类型。
我究竟做错了什么?
如果要使用表达式树,则需要将表达式树本身传递给 LINQ 方法:
return ctx.Customers.AsExpandable().Where(condition)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)