Expression.IsFalse是未知的?

Caf*_*eek 2 linq expression-trees

我最终尝试运行查询时收到以下错误

"IsFalse"类型的未知LINQ表达式

这是代码

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;
}
Run Code Online (Sandbox Code Playgroud)

重要变量具有以下值

query: an IQueryable that I am building up
type: String
methodName: "EndsWith"
rule.Op: "ne" //Not Ends With
op: "ew"
value: "somestring"
Run Code Online (Sandbox Code Playgroud)

基本上,如果op和rule.Op相等,它只运行methodName(EndsWith)并相应地过滤.但是,如果它们不同,我想否定结果.

Jon*_*Jon 6

看来你没有做错什么; 您的LINQ提供程序根本不知道如何处理Expression.IsFalse返回的表达式树实例,因此它会抱怨.

您可以尝试自己手动构建"is false"表达式树,这应该可以工作:

Expression.Lambda<Func<T, bool>>(
   Expression.Equal(call, Expression.Constant(false)),
   parameter)
Run Code Online (Sandbox Code Playgroud)