I have a search form that allows users to search on several different fields in several different ways. Here is an example of my code.
var claims = from c in db.
You could make a dictionary of your possible predicates:
Dictionary>>> map = new Dictionary>>>() {
{ "StartsWith", t => c => c.companyFileID.StartsWith(t) },
{ "Equals", t => c => c.companyFileID == t },
{ "Contains", t => c => c.companyFileID.Contains(t) }
};
Which could be used like this:
var search = ddlSearchField.Text;
var text = txtSearchBox.Text;
var claims = from c in db.Claims select c;
Func>> predicate = null;
if(dict.TryGetValue(search, out predicate))
claims = claims.Where(predicate(text));