Home > Net >  I get a 'must be reducible node' error when calling `Compile()` on a Lamda Expression from
I get a 'must be reducible node' error when calling `Compile()` on a Lamda Expression from

Time:06-09

I upgraded to from EFCore 3 to EFCore 6 and I get this error must be reducible node. I use Postgresql has my database.

The error come from the Compile statement:

private IQueryable<T> ApplyFilter(IQueryable<T> query, string rawFilter, ODataQueryOptionParser oDataQueryOptionParser, ODataQueryContext oDataQueryContext)
{
  FilterQueryOption filter = new FilterQueryOption(rawFilter, oDataQueryContext, oDataQueryOptionParser);
  var queryable = filter.ApplyTo(query, new ODataQuerySettings());

  var visitor = new CaseInsensitiveAccentSensitiveExpressionVisitor();
  var queryExpression = queryable.Expression;
  var visitExpression = visitor.Visit(queryExpression);

  queryable = (IQueryable)Expression.Lambda(visitExpression).Compile().DynamicInvoke();
  return (IQueryable<T>)queryable;
}

At first I thought it was in my CaseInsensitiveAccentSensitiveExpressionVisitor class but, the thing is, even if my CaseInsensitiveAccentSensitiveExpressionVisitor only call base functions whitout any more codre, I still get this error.

public class CaseInsensitiveAccentSensitiveExpressionVisitor : ExpressionVisitor
{
  protected override Expression VisitMethodCall(MethodCallExpression node)
  {
    return base.VisitMethodCall(node);
  }

  protected override Expression VisitMember(MemberExpression node)
  {
    return base.VisitMember(node);
  }
}

The CaseInsensitiveAccentSensitiveExpressionVisitor is suppose to add ToLower to string fields so filter are case Insensitive.

CodePudding user response:

Use query.Provider.CreateQuery<>(..) for generating IQueryable<> with corrected expression.

private IQueryable<T> ApplyFilter(IQueryable<T> query, string rawFilter, ODataQueryOptionParser oDataQueryOptionParser, ODataQueryContext oDataQueryContext)
{
    FilterQueryOption filter = new FilterQueryOption(rawFilter, oDataQueryContext, oDataQueryOptionParser);
    var queryable = filter.ApplyTo(query, new ODataQuerySettings());

    var visitor = new CaseInsensitiveAccentSensitiveExpressionVisitor();
    var queryExpression = queryable.Expression;
    var visitExpression = visitor.Visit(queryExpression);

    return query.Provider.CreateQuery<T>(visitExpression);
}
  • Related