Search code examples
linqexpression-trees

Expression.IsFalse is Unknown?


I am getting the following error when I ultimately try to run the query

Unknown LINQ expression of type 'IsFalse'

this is the code

private static IQueryable<T> QueryMethod<T>(
    IQueryable<T> query,
    QueryableRequestMessage.WhereClause.Rule rule,
    Type type,
    string methodName,
    Expression property,
    Expression value,
    string op,
    ParameterExpression parameter
) where T : class
{
    var methodInfo = type.GetMethod(methodName, new[] { type });
    var call = Expression.Call(property, methodInfo, value);
    var expression = rule.Op.Equals(op)
                            ? Expression.Lambda<Func<T, bool>>(call, parameter)
                            : Expression.Lambda<Func<T, bool>>(Expression.IsFalse(call), parameter);
    query = query.Where(expression);
    return query;
}

The important variables have the following values

query: an IQueryable that I am building up
type: String
methodName: "EndsWith"
rule.Op: "ne" //Not Ends With
op: "ew"
value: "somestring"

Basically, if op and rule.Op are equal, it just runs the methodName (EndsWith) and filters accordingly. However, If they are different, I want to negate the result.


Solution

  • It seems that you are not doing anything wrong; your LINQ provider simply does not know how to deal with the expression tree instance that Expression.IsFalse returns so it complains.

    You can try to manually construct the "is false" expression tree yourself, which should work:

    Expression.Lambda<Func<T, bool>>(
       Expression.Equal(call, Expression.Constant(false)),
       parameter)