IT Share you

C # PredicateBuilder 엔터티 : 매개 변수 'f'가 지정된 LINQ to Entities 쿼리 식에 바인딩되지 않았습니다.

shareyou 2021. 1. 9. 10:52
반응형

C # PredicateBuilder 엔터티 : 매개 변수 'f'가 지정된 LINQ to Entities 쿼리 식에 바인딩되지 않았습니다.


동적 필터를 구축해야했고 엔티티를 계속 사용하고 싶었습니다. 이런 이유로 저는 albahari의 PredicateBuilder를 사용하고 싶었습니다.

다음 코드를 만들었습니다.

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

코드를 실행했을 때 날짜 필터가 아닌 필터는 1 개뿐이었습니다. 따라서 내부 술어 만 채워졌습니다. 술어가 실행되었을 때 다음 오류가 발생했습니다.

매개 변수 'f'가 지정된 LINQ to Entities 쿼리 식에 바인딩되지 않았습니다.

답변을 검색하는 동안 다음 페이지를 찾았습니다 . 그러나 이것은 이미 LINQKit에서 구현되었습니다.

다른 사람이이 오류를 경험했으며 해결 방법을 알고 있습니까?


동일한 오류가 발생했는데 PredicateBuilder로 만든 조건자가 PredicateBuilder로 만든 다른 조건부로 구성된 조건자가있을 때 문제가 발생한 것 같습니다.

예를 들어 (A OR B) AND (X OR Y) 한 빌더가 A OR B를 작성하고 하나는 X OR Y를 작성하고 세 번째는 이들을 AND로 만듭니다.

한 수준의 조건 자 AsExpandable 만 제대로 작동했지만 둘 이상의 수준이 도입되었을 때 동일한 오류가 발생했습니다.

나는 어떤 도움도 찾을 수 없었지만 시행 착오를 통해 일을 할 수있었습니다. 술어를 호출 할 때마다 Expand 확장 메서드를 사용했습니다.

다음은 단순성을 위해 잘라낸 약간의 코드입니다.

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는 이미 AsExpandable이 호출 된 IQueryable이고 ConstructOptionNotMatchPredicate는 Expression을 반환합니다.

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.


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:

// 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! :-)

ReferenceURL : https://stackoverflow.com/questions/2947820/c-sharp-predicatebuilder-entities-the-parameter-f-was-not-bound-in-the-specif

반응형