InvalidOperationException: No method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied arguments

后端 未结 2 1336
無奈伤痛
無奈伤痛 2021-01-19 05:11

(Code below has been updated and worked properly)

There is dynamic OrderBy sample from LinqPad. What I want to do is just simply apply \'Where\' rather than \'OrderB

相关标签:
2条回答
  • 2021-01-19 06:01

    Your lambda expression creation looks odd to me. You're adding another parameter for no obvious reason. You're also using Predicate<Purchase> instead of Func<Purchase, bool>. Try this:

    LambdaExpression lambda = Expression.Lambda<Func<Purchase, bool>>(
                        Expression.GreaterThan(member, Expression.Constant(100)), 
                        purchaseParam);
    
    0 讨论(0)
  • 2021-01-19 06:11
    1. Use the lambda that Jon Skeet supplied. Perhaps he can also explain why ParameterExpression is so painful to use and requires using the same instance, instead of being able to be matched by name :)

    2. Modify this line:

    Type[] exprArgTypes = { query.ElementType };
    

    exprArgTypes is type parameters to

    IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate).
    

    As you can see it only has one type parameter - TSource, which is Purchase. What you were doing, effectively, was calling Where method with two type parameters like below:

    IQueryable<Purchase> Where<Purchase, bool>(this IQueryable<Purchase> source, Expression<Func<Purchase, bool>> predicate)
    

    Once both of those fixes are in the expression runs with no problem.

    0 讨论(0)
提交回复
热议问题