What you really need is to have a method for translated Expression<Func<T, string>>
to Expression<Func<T, bool>>
:
public static Expression<Func<T, bool>> GetContainsExpression<T>(Expression<Func<T, string>> prop, string searchString)
{
var method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(searchString, typeof(string));
var containsMethodExp = Expression.Call(prop.Body, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, prop.Parameters[0]);
}
This is inspired by this other answer : How do I create an expression tree to represent 'String.Contains(“term”)' in C#?
var predicates = new List<Expression<Func<Customer, bool>>>();
predicates.Add(GetContainsExpression((Customer x) => x.Name, this.Name));
predicates.Add(GetContainsExpression((Customer x) => x.Description, this.Description));