实体框架 - 动态组合谓词



在执行EF查询时,将类似的谓词组合起来会导致堆栈溢出异常。这是怎么了?

Func<Item, bool> wherePredicate = i => i.isDeleted;
Func<Item, bool> wherePredicate2 = i => i.isExpired;
wherePredicate = i=> wherePredicate(i) || wherePredicate2(i);

您已经创建了递归循环,可能是偶然的。与:

wherePredicate = i=> wherePredicate(i) || wherePredicate2(i);

您只是定义匿名函数将调用i=> wherePredicate(i) (...),因此可以调用自己。诸如"'='右侧的所有内容"之类的常见规则在分配之前将调用。P>

还有(您标记为实体框架) - 它在.Where()子句中无法使用。简短答案 - EF不知道如何在数据库中调用Func<>。长答案 - 请参阅Linqkit,它将帮助您定义可重复使用的谓词。奇怪的偶然 - 您的问题几乎在"组合表达"部分中从字面上解释了。

最新更新