(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
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);
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 :)
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.