Using Predicate Builder of LINQKit for Or-clauses in LINQ

26.02.2016

LINQ does not support Or-clauses by default. As I need this functionality in my current project, I am using the Predicate Builder which is part of the LINQKit. This class allows you to add an Or-clause to your LINQ-Query:

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

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

This is very helpful!

You can find LINQKit on NuGet.

Zurück zur Übersicht

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

*Pflichtfelder

*