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.