I have a list of Linq expressions like:
List<Expression<Func<Customer, bool>>>
I need to add a whole bunch of predicates from a search page, like:
x.Name.Contains(searchString)
x.Description.Contains(searchString)
...
I want to create a method so I don't end up with a mass of duplicated code. Something with a signature like:
void AddCustomerPredicate(List<Expression<Func<Customer, bool>>> predicates, Expression<Func<Customer, string>> prop, string searchString);
Which I would use like:
var predicates = new List<Expression<Func<Customer, bool>>>();
AddCustomerPredicate(predicates, x => x.Name, this.Name);
AddCustomerPredicate(predicates, x => x.Description, this.Description);
...
I've simplified the problem a bit, but that is the gist of it. I haven't done much work with expression trees and the like, so I'm not sure how to implement this method?
**EDIT**
I might have over simplified the problem too much. I know how to add to the list like predicates.Add(x => x.Name.Contains(this.searchString))
, but I have various things I want to do on each search parameter before adding it to the list (e.g. check for null or empty). I therefore want to call a method on each search parameter as above, so all of that stuff can be contained in a single method.