I think you just need to change the type lambda
ParameterExpression parameter = Expression.Parameter(typeof(Product), "x");
MemberExpression Left = Expression.MakeMemberAccess(parameter, typeof(Product).GetProperty("Name"));
ConstantExpression Right = Expression.Constant(value, typeof(String));
BinaryExpression expression = Expression.Equal(Left, Right);
Expression< Func< Product, bool > > lambda = Expression.Lambda<Func<Product, bool>>(expression, parameter);
Now it Expression<Func<Product, bool>>and IQueryable.Wheretakes it as an argument. Expression.Lambda<TDelegate>returns a TDelegate, which is also LambdaExpression, therefore compiling Expression.Lambdain your case and in my case, but IQueryable.Wherewants to see it as Expression<Func<Product, bool>>.
Sort of:
List< Product > products = new List< Product >
{
new Product { Name = "bar" },
new Product { Name = "foo" }
};
IQueryable< Product > queryable = products.AsQueryable().Where( lambda );
source
share