Thursday, December 6, 2012

Creating Dynamic Predicates Using Predicate Builder

IQueryable SearchProducts (params string[] keywords)
{
  var predicate = PredicateBuilder.False();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
  return dataContext.Products.Where (predicate);
}


see C# in a Nutshell

No comments: