0

I have code that is creating LINQ query from string, this is core of it:

foreach (string singleFilterQuery in splittedFilter)
{
    var expr = GetPredicate<T>(singleFilterQuery);

    if (expr == null)
    {
        continue;
    }

    if (expression == null)
    {
        expression = expr;
    }
    else
    {
        var body = Expression.AndAlso(expression.Body, expr.Body);
        expression = Expression.Lambda<Func<T, bool>>(body, expression.Parameters[0]);
    }
}

Then there is

items = items.Where(expressionFilter);

and

await query.Skip(skip).Take(pageSize).ToListAsync();

Query.Expression.DebugView:

.Call System.Linq.Queryable.OrderBy(
    .Call System.Linq.Queryable.Where(
        .Extension<Microsoft.EntityFrameworkCore.Query.QueryRootExpression>,
        '(.Lambda #Lambda1<System.Func`2[Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData,System.Boolean]>)),
    '(.Lambda #Lambda2<System.Func`2[Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData,System.Guid]>))

.Lambda
#Lambda1<System.Func`2[Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData,System.Boolean]>(Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData $x) {
    $x.Roe >= 0,1D && $x.Roic >= 0,1D }

.Lambda
#Lambda2<System.Func`2[Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData,System.Guid]>(Fundamentalnie.Domain.CompanyData.Entities.CompanyShortData $var1) {
    $var1.Id }

When my filter is having 1 "where" it works fine. When there are 2 "where" or more, I get this error. If I look up linq query before executing, it looks good (eg. Where(x => x.Roe > 1 && x.Roi > 1)).

System.InvalidOperationException: The LINQ expression
'DbSet()
.Where(c => c.Roe >= 0,1 && x.Roic >= 0,1)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

Is it an EF Core bug?

EF Core is the latest EF Core 5 version.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Piotr M
  • 479
  • 3
  • 9
  • 26
  • Beautiful, works like a charm. My case are dynamic filters not generic repos, just fyi :). Thank you – Piotr M Jan 07 '22 at 16:07

0 Answers0