简体   繁体   English

C# PredicateBuilder Entities:在指定的 LINQ to Entities 查询表达式中未绑定参数“f”

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

I needed to build a dynamic filter and I wanted to keep using entities.我需要构建一个动态过滤器,我想继续使用实体。 Because of this reason I wanted to use the PredicateBuilder from albahari.由于这个原因,我想使用来自 albahari 的 PredicateBuilder。

I created the following code:我创建了以下代码:

var invoerDatums = PredicateBuilder.True<OnderzoeksVragen>();
var inner = PredicateBuilder.False<OnderzoeksVragen>();

foreach (var filter in set.RapportInvoerFilter.ToList())
{
    if(filter.IsDate)
    {
        var date = DateTime.Parse(filter.Waarde);
        invoerDatums = invoerDatums.Or(o => o.Van >= date && o.Tot <= date);
    }
    else
    {
        string temp = filter.Waarde;
        inner = inner.Or(o => o.OnderzoekType == temp);
    }
}

invoerDatums = invoerDatums.And(inner);
var onderzoeksVragen = entities.OnderzoeksVragen
                               .AsExpandable()
                               .Where(invoerDatums)
                               .ToList();

When I ran the code there was only 1 filter which wasn't a date filter.当我运行代码时,只有 1 个过滤器不是日期过滤器。 So only the inner predicate was filled.所以只填充了内部谓词。 When the predicate was executed I got the following error.执行谓词时,出现以下错误。

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

While searching for an answer I found the following page .在寻找答案时,我找到了以下页面 But this is already implemented in the LINQKit.但这已经在 LINQKit 中实现了。

Does anyone else experienced this error and know how to solve it?有没有其他人遇到过这个错误并知道如何解决它?

I ran across the same error, the issue seemed to be when I had predicates made with PredicateBuilder that were in turn made up of other predicates made with PredicateBuilder我遇到了同样的错误,问题似乎是当我用 PredicateBuilder 制作的谓词又由 PredicateBuilder 制作的其他谓词组成时

eg (A OR B) AND (X OR Y) where one builder creates A OR B, one creates X OR Y and a third ANDs them together.例如 (A OR B) AND (X OR Y),其中一个构建器创建 A OR B,一个创建 X OR Y,第三个将它们 AND 在一起。

With just one level of predicates AsExpandable worked fine, when more than one level was introduced I got the same error.只有一层谓词 AsExpandable 工作正常,当引入不止一层时,我得到了同样的错误。

I wasn't able to find any help but through some trial and error I was able to get things to work.我无法找到任何帮助,但通过一些反复试验,我能够使事情正常进行。 Every time I called a predicate I followed it with the Expand extension method.每次我调用谓词时,我都会使用 Expand 扩展方法跟随它。

Here is a bit of the code, cut down for simplicity:这是一些代码,为简单起见,将其删减:

public static IQueryable<Submission> AddOptionFilter(
    this IQueryable<Submission> query, 
    IEnumerable<IGrouping<int, int>> options)
{
    var predicate = options.Aggregate(
        PredicateBuilder.False<Submission>(),
        (accumulator, optionIds) => accumulator.Or(ConstructOptionMatchPredicate(optionIds).Expand()));
        query = query.Where(predicate.Expand());            
    return query;
}

Query is an IQueryable which has already had AsExpandable called, ConstructOptionNotMatchPredicate returns an Expression. Query 是一个已经调用了 AsExpandable 的 IQueryable,ConstructOptionNotMatchPredicate 返回一个表达式。

Once we got past the error we were certainly able to build up complicated filters at runtime against the entity framework.一旦我们克服了错误,我们当然能够在运行时针对实体框架构建复杂的过滤器。

Edit:编辑:

Since people are still commenting on and up voting this I assume it is still useful so I am sharing another fix.由于人们仍在对此进行评论和投票,我认为它仍然有用,因此我正在分享另一个修复程序。 Basically I have stopped using LinqKit and it's predicate builder in favour of this Universal Predicate Builder that has the same API but doesn't need Expand calls, well worth checking out.基本上我已经停止使用 LinqKit 并且它的谓词构建器有利于这个具有相同 API 但不需要扩展调用的通用谓词构建器,非常值得一试。

I got this error and Mant101's explanation got me the answer, but you might be looking for a simpler example that causes the problem:我收到此错误,Mant101 的解释为我提供了答案,但您可能正在寻找导致问题的更简单示例:

// This predicate is the 1st predicate builder
var predicate = PredicateBuilder.True<Widget>();

// and I am adding more predicates to it (all no problem here)
predicate = predicate.And(c => c.ColumnA == 1);
predicate = predicate.And(c => c.ColumnB > 32);
predicate = predicate.And(c => c.ColumnC == 73);

// Now I want to add another "AND" predicate which actually comprises 
// of a whole list of sub-"OR" predicates
if(keywords.Length > 0)
{
    // NOTICE: Here I am starting off a brand new 2nd predicate builder....
    // (I'm not "AND"ing it to the existing one (yet))
    var subpredicate = PredicateBuilder.False<Widget>();

    foreach(string s in keywords)
    {
        string t = s;  // s is part of enumerable so need to make a copy of it
        subpredicate = subpredicate.Or(c => c.Name.Contains(t));
    }

    // This is the "gotcha" bit... ANDing the independent
    // sub-predicate to the 1st one....

    // If done like this, you will FAIL!
//  predicate = predicate.And(subpredicate); // FAIL at runtime!

    // To correct it, you must do this...
    predicate = predicate.And(subpredicate.Expand());  // OK at runtime!
}

Hope this helps!希望这可以帮助! :-) :-)

暂无
暂无

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

相关问题 嵌套PredicateBuilder谓词:“参数&#39;f&#39;在指定的LINQ to Entities查询表达式中未绑定” - Nesting PredicateBuilder predicates : '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