I see a lot of code similar to the following
var customrs = MyDataContext.Customers.Where(...);
if (!String.IsNullOrEmpty(input)) {
customers = customers.Where(c => c.Email.Contains(input));
}
I would like to put this in an extension method that checks the input is valid before invoking Where on IQueryable so that it can be called like
customers = MyDataContext.Customers.Where(...)
.ContainsText(c => c.Email, input);
My extension method looks like this
public static IQueryable<T> ContainsText<T>(this IQueryable<T> source, Expression<Func<T, string>> selector, string text) {
if (String.IsNullOrEmpty(text) {
return source;
}
else {
//do something here
}
}
How can I call Contains() on the expression parsed? Or is there another way to return IQueryable of results where the expression parsed contains the text parsed?
Update: It's for Linq to Sql