What is the best way to assemble a dynamic WHERE
clause to a LINQ statement?
I have several dozen checkboxes on a form and am passing them back as: Dictionary<string, List<string>>
(specifically, Dictionary<fieldName, List<values>>
) to my LINQ query.
public IOrderedQueryable<ProductDetail> GetProductList(string productGroupName, string productTypeName, Dictionary<string,List<string>> filterDictionary)
{
var q = from c in db.ProductDetail
where c.ProductGroupName == productGroupName && c.ProductTypeName == productTypeName
// insert dynamic filter here
orderby c.ProductTypeName
select c;
return q;
}
Dictionary<string,List<string>>
pairs. The key is obviously a property/field, but how should its value be compared to the strings inList<string>
? And should the predicates be additional (&&
) or extended (||
)? Without such details this question can't be answered and most of the answers even totally ignored this part.