【问题标题】:How to use predicate builder with linq2sql and OR operator如何使用带有 linq2sql 和 OR 运算符的谓词生成器
【发布时间】:2010-04-22 11:26:20
【问题描述】:

我有两个表(TABLE1、TABLE2 - 我知道是唯一的),它们分别具有一对多的关系,并且两个表的 ID 列之间都有一个外键。

使用 linq2sql 我正在尝试选择所有 TABLE1 条目,以便它们对应的 TABLE2 值在我通过的列表中至少包含一项它。

这是我在 LINQPad(很棒的程序)中使用的一些示例代码来测试它,但是我得到了错误 NotSupportedException: Unsupported overload used for query operator '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。

【问题讨论】:

标签: c# linq linq-to-sql predicate predicatebuilder


【解决方案1】:

解决方法是

  1. TABLE1 上调用 AsExpandable() 对象
  2. 对表达式调用 Compile() 变量,用于 EntitySet 时。

所以你的最终查询是

IQueryable query = 
    from t1 in TABLE1.AsExpandable()
    where t1.TABLE2.Any(predicate.Compile()) //the problem should disappear
    select a;

更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多