C#predicateBuilder实体:参数'F'在指定的LINQ到实体没有义务查询前pression [英] C# PredicateBuilder Entities: The parameter 'f' was not bound in the specified LINQ to Entities query expression

查看:419
本文介绍了C#predicateBuilder实体:参数'F'在指定的LINQ到实体没有义务查询前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个动态过滤器,我想用实体保持。因为这个原因,我想用predicateBuilder从阿尔巴哈利。

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.

我创建了以下code:

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();

当我跑了code,只有1这是不是一个日期过滤器过滤。因此,只有内predicate弥漫。当在执行predicate我得到了下面的错误。

When I ran the code there was only 1 filter which wasn't a date filter. So only the inner predicate was filled. When the predicate was executed I got the following error.

参数'F'在没有约束
  指定的LINQ to Entities查询
  前pression。

The parameter 'f' was not bound in the specified LINQ to Entities query expression.

在寻找一个答案,我发现下面的<一个href=\"http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-$p$pdicates.aspx\">page.但是,这是在LINQKit已经实施。

While searching for an answer I found the following page. But this is already implemented in the LINQKit.

难道其他人遇到这个错误,并知道如何解决呢?

Does anyone else experienced this error and know how to solve it?

推荐答案

我碰到了同样的错误跑了,这个问题似乎是,当我有predicateBuilder取得predicates那名依次由其他的以predicateBuilder取得predicates

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

例如。 (A或B)和(X或Y),其中一个生成器所创建A或B,一是创建X或Y和第三AND将它们放在一起。

e.g. (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.

只需一个水平predicates的AsExpandable工作得很好,当引入一个以上的水平,我得到了同样的错误。

With just one level of predicates AsExpandable worked fine, when more than one level was introduced I got the same error.

我无法找到任何帮助,但通过一些试验和错误我能得到的东西的工作。
每次我叫了predicate我展开扩展方法跟着它。

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.

下面是位code的,降低为简单:

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;
}

查询是一个IQueryable它已经过AsExpandable叫,ConstructOptionNotMatch predicate返回前pression。

Query is an IQueryable which has already had AsExpandable called, ConstructOptionNotMatchPredicate returns an Expression.

一旦我们得到过去的错误我们肯定能够在对实体框架运行时建立复杂的过滤器。

Once we got past the error we were certainly able to build up complicated filters at runtime against the entity framework.

编辑:

由于人还在评论和投票了这个我认为它仍然是有用的,所以我分享另一个修补程序。基本上我已经使用LinqKit停了下来,这是有利于这个的通用$ p $具有相同的API,但并不需要展开来电,非常值得一试。pdicate构建器

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.

这篇关于C#predicateBuilder实体:参数'F'在指定的LINQ到实体没有义务查询前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆