Reputation: 169
I have a method that passes a field that belongs to type T, and then an operator like Contains
or Equals
or StartsWith
I have this code so far:
IQueryable<Review> queryable = this.rvCurReviewSet.AsQueryable<Review>();
var paramExp = Expression.Parameter(typeof(Review), "review");
var propExp = Expression.Property(paramExp, "ProductID");
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var val = Expression.Constant(input, typeof(string));
var containsExp = Expression.Call(propExp, method, val);
Console.WriteLine(queryable.Provider.CreateQuery<Review>(containsExp).Count());
When I run this I get the error: Additional information: Argument expression is not valid
Basically, I want to see if a field in Review satisfies the criteria. So, for example I would find Review.ProductID contains "123"
or Review.ProductID startsWith "123"
and so on.
Upvotes: 1
Views: 359
Reputation: 27357
You were nearly there - but you need to pass it a lambda expression..
var lam = Expression.Lambda<Func<Review, bool>>(containsExp, paramExp);
Console.WriteLine(queryable.Provider.CreateQuery<Review>(lam).Count());
What you had was a body expression, which operated on review
, but didn't specify where the value for review
comes from.
Upvotes: 2