简体   繁体   English

嵌套PredicateBuilder谓词:“参数'f'在指定的LINQ to Entities查询表达式中未绑定”

[英]Nesting PredicateBuilder predicates : 'The parameter 'f' was not bound in the specified LINQ to Entities query expression'

I am building predicates using LinqKit's PrediateBuilder class to dynamically setup filters and I want to combine a nested one to another . 我正在使用LinqKit的PrediateBuilder类构建谓词,以动态设置过滤器,并且我想将嵌套的一个与另一个结合

I have read this ( http://www.albahari.com/nutshell/predicatebuilder.aspx ) : 我已经阅读了以下内容( http://www.albahari.com/nutshell/predicatebuilder.aspx ):

在此处输入图片说明

Here is my code : 这是我的代码:

// The main predicate.
var mainPredicate = PredicateBuilder.True<Document>();

// ... some other conditions to the main predicate here ...

// The inner predicate (combined conditions using OR).
var innerPredicate = PredicateBuilder.False<Document>();

foreach (var period in periods)
{
    var p = period;
    innerPredicate =
        innerPredicate.Or(
            d =>
            (d.Date >= p.DateFrom && d.Date <= p.DateTo));
}

mainPredicate = mainPredicate.And(innerPredicate);

documents = this.ObjectSet.AsExpandable().Where(mainPredicate).ToList();

I am combining my two predicates just like it is explained in the documentation. 我将两个谓词组合在一起,就像文档中所解释的那样。 However, I get this exception : 但是,我得到这个异常:

The parameter 'f' was not bound in the specified LINQ to Entities query expression 在指定的LINQ to Entities查询表达式中未绑定参数“ f”

I first thought that the inner predicate has to be expanded before combining it with the main predicate, so I changed my combining code to add a call to to the inner predicate's Expand method like this : 我首先认为内部谓词在与主谓词组合之前必须先进行扩展,因此我更改了组合代码,以添加对内部谓词的Expand方法的调用,如下所示:

mainPredicate = mainPredicate.And(innerPredicate.Expand());

But I get the exact same exception. 但是我得到了完全一样的例外。

The only difference in my code versus the documentation is that I dynamically build my nested predicate using a foreach loop . 我的代码与文档之间的唯一区别是,我使用foreach循环动态构建了嵌套谓词。 I just don't know how it can negatively affect the resulting expression. 我只是不知道它如何对结果表达式产生负面影响。

  • What is wrong with my code ? 我的代码有什么问题?

  • How can I actually debug this ? 我该如何实际调试呢?

  • Where the f parameter comes from ? f参数来自哪里? How is it generated ? 它是如何产生的? Why is it problematic in my case ? 为什么在我的情况下有问题?

  • Is there some kind of expression tree visualizer of some kind that could help me actually see what is wrong with the resulting expression ? 是否存在某种形式的表达树可视化工具,可以帮助我实际查看结果表达式的问题? Because the expression's body is hard to read. 因为表达式的正文很难阅读。

Finally, I have found a way to avoid combining multiple predicates to the main expression tree. 最后,我找到了一种避免将多个谓词组合到主表达式树的方法。

Given that each predicate represents a different filter and I want the final, combined filter to be a series of must-be-respected conditions, we can say that each of the predicates has to return true for the final predicate to return true. 假设每个谓词代表一个不同的过滤器,并且我希望最终的组合过滤器成为一系列必须遵守的条件,我们可以说每个谓词都必须返回true ,最终谓词才能返回true。

For that to work, the predicates has to be combined with AND . 为此,谓词必须与AND结合使用。 So, the resulting SQL query must look like this : 因此,生成的SQL查询必须如下所示:

predicate1 AND predicate2 AND predicate3 ... predicate1 AND predicate2 AND predicate3 ...

A better way to combine these predicates with AND is to chain Where query operators to the final query, like this : 将这些谓词与AND组合的更好方法是将Where查询运算符链接到最终查询,如下所示:

var documents = this.ObjectSet.AsExpandable()
    .Where(mainPredicate)
    .Where(otherPredicate)
    .Where(yetAnotherPredicate)
    .ToList();

The resulting SQL query will combine each of these predicates with AND . 结果SQL查询将把这些谓词中的每一个与AND组合在一起。 That is just what I wanted to do. 那就是我想做的。

It is easier than hacking out an expression tree by myself. 这比我自己破解一个表达式树要容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# PredicateBuilder Entities:在指定的 LINQ to Entities 查询表达式中未绑定参数“f” - C# PredicateBuilder Entities: The parameter 'f' was not bound in the specified LINQ to Entities query expression &#39;参数&#39;f&#39;未绑定在指定的LINQ to Entities查询表达式中&#39; - 'The parameter 'f' was not bound in the specified LINQ to Entities query expression' 参数“***”未绑定在指定的LINQ to Entities查询表达式中 - The parameter '***' was not bound in the specified LINQ to Entities query expression 得到错误“参数未在指定的LINQ to Entities查询表达式中绑定。” - Getting error “The parameter was not bound in the specified LINQ to Entities query expression.” 在指定的LINQ to Entities查询表达式中未绑定参数“ p” - The parameter 'p' was not bound in the specified LINQ to Entities query expression 组合AndAlso在指定的LINQ to Entities查询表达式中未绑定参数&#39;foo&#39; - Combining AndAlso The parameter 'foo' was not bound in the specified LINQ to Entities query expression 参数“d”未绑定在指定的LINQ to Entities查询表达式中 - The parameter 'd' was not bound in the specified LINQ to Entities query expression “参数未绑定在指定的 LINQ to Entities 查询表达式中。” 规格图案及 - "The parameter was not bound in the specified LINQ to Entities query expression." Specification Pattern And LINQ表达式树-在指定的LINQ to Entities查询表达式中未绑定参数“ x” - LINQ Expression Tree - The parameter 'x' was not bound in the specified LINQ to Entities query expression 使用LINQ使用PredicateBuilder对实体进行动态查询 - Dynamic query with linq to entities using PredicateBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM