【发布时间】:2013-05-01 23:45:46
【问题描述】:
我正在使用 LinqKit,我想编写一个谓词,其中代码必须调用一个普通的布尔方法,如下所示:
var predicate = PredicateBuilder.False<MyEntity>();
var predicate = predicate.And(myEntity => this.EntityMatches(myEntity));
this.ObjectSet.AsExpandable().Where(predicate).ToList();
这是 EntityMatches 方法的(部分):
private bool EntityMatches(MyEntity myEntity)
{
bool isMatch;
// I make a call to another library boolean method here.
isMatch = otherLib.IsEntityMatch(myEntity);
// I perform some other checks right after.
isMatch &= // some other conditions ...
return isMatch;
}
运行延迟执行时出现此异常:
LINQ to Entities 无法识别方法 'Boolean EntityMatches(MyEntity)' 方法,并且这个方法不能翻译 到商店表达式中。
如何重写 EntityMatches 方法以便商店提供者可以理解?
【问题讨论】:
-
与 linqkit 无关的问题。您的函数适用于 IEnumerable 列表,但不适用于 IQueryable,因为 Linq 无法转换自定义方法
-
您在哪里看到 IEnumerable 或 IQueryable 类型?谓词变量的类型为
Expression<Func<MyEntity, bool>>。
标签: c# linq-to-entities linq-expressions linqkit