2013-02-12 22 views
1

我收到以下錯誤,當我最終嘗試運行的查詢類型的Expression.IsFalse是未知的?

未知LINQ表達式「IsFalse」

這是代碼

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; 
} 

的重要變量有以下值

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

基本上,如果op和rule.Op相等,它只是運行methodName(EndsWith)並相應地進行過濾。但是,如果它們不同,我想否定結果。

回答

5

看來你沒有做錯任何事;你的LINQ提供者根本不知道如何處理Expression.IsFalse返回的表達式樹實例,所以它抱怨。

您可以嘗試手動構造 「是假的」 表達式樹自己,這應該工作:

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

的Microsoft SQL Server 2008(SP3) – CaffGeek 2013-02-12 15:19:38

+1

@CaffGeek:這不是LINQ提供程序。提供者是Linq to Entities,Linq to SQL等等。 – Jon 2013-02-12 15:20:37

+0

比較虛假的作品,謝謝。 – CaffGeek 2013-02-12 15:24:59