我有两个表(TABLE1、TABLE2 - 我知道是唯一的),它们分别具有一对多关系,并且两个表的 ID 列之间有一个外键。
使用 linq2sql 我试图选择所有 TABLE1 条目,以便它们相应的 TABLE2 值包含我传递的列表中的至少 1 个项目
这是我在 LINQPad(很棒的程序)中使用的一些示例代码来对其进行测试,但是我收到错误 NotSupportedException:不支持的重载用于查询运算符“Any”。
long[] items = { 3, 5, 8 };
var predicate = PredicateBuilder.False<TABLE2>();
foreach (long i in items)
{
long t = i;
predicate = predicate.Or(att => att.ID == t);
}
//TABLE2.Where(predicate).Dump(); //works like a charm
IQueryable query =
from t1 in TABLE1
where t1.TABLE2.AsQueryable().Any(predicate) //problem with this line
select a;
query.Dump();
更新
在 LinqPad 中使用 LinqKit 时,添加对 LinqKit.dll 的引用,取消选中 Include PredicateBuilder,然后在 Additional Namespace Imports 选项卡下添加 LinqKit。
最佳答案
解决方法是
- 在
TABLE1
上调用 AsExpandable() 对象 - 对表达式调用 Compile() 变量,当用于 EntitySet 时。
所以你的最终查询是
IQueryable query =
from t1 in TABLE1.AsExpandable()
where t1.TABLE2.Any(predicate.Compile()) //the problem should disappear
select a;
更多信息 here .
关于c# - 如何将谓词生成器与 linq2sql 和 OR 运算符一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690298/