LinqKit vs 'Method不能翻译成store表达式'例外
本文关键字:store 表达式 例外 Method vs LinqKit 不能 翻译 | 更新日期: 2023-09-27 18:11:44
我正在使用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不识别布尔方法EntityMatches(MyEntity)'方法,该方法无法翻译
如何重写EntityMatches方法,使其能够被存储提供者理解?
LinqKit不是魔法,它不能看到EntityMatches()
或otherLib.IsEntityMatch()
内部,所以没有办法它可以帮助你将这些转换成SQL。
LinqKit所能做的就是用简单的部分创造一个复杂的条件。但每个简单的部分都必须自己翻译成SQl,我不知道如何在你的情况下做到这一点,特别是因为你正在使用一些外部库。
您应该将您的方法转换为Linq表达式。请阅读这可能会有所帮助linq2entities的可查询扩展方法