score:2

Accepted answer

you need to make use of PredicateBuilder, If i am getting correctly you are making single query and you need conditions in it

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);
}

Dynamically Composing Expression Predicates

score:0

You could use the System.Linq.Dynamic package available on Nuget. You can also follow this guide on how to use the package.

In your case, I believe the query could be:

var query = IQueryable<DataType>();
foreach(string parameter in searchParameters)
{
   query = query.Where("fieldName ==" + parameter );
}

// call the LINQ query at the end when all the conditions are built
var results = query.Select(q => q.field).ToList();

This may work for your case, otherwise you could use a predicate builder as per Pranay's suggestion.


Related Articles