C#: Predicates in LINQ to Entities
If you started programming MVC, in particular the Entity Framework Model, and find yourself filtering your data model using the **Where
// using query syntax - calling Where method once
carQuery.Where(c => c.Color == ‘red’ && c.Price < 10000);
// using method chaining - calling Where method multiple times
carQuery.Where(c => c.Color == ‘red’).Where(c => c.Price < 10000);
// method chaining is useful in a loop
foreach (KeyValuePair<string, string> filter in filters)
{
string filterVal = filter.Value;
switch (filter.Key)
{
case “Color”:
carQuery = carQuery.Where(c => c.Color == filterVal).
break;
case “Price”:
int price = Int32.Parse(filterVal);
carQuery = carQuery.Where(c => c.Price < price).
break;
}
}
There are other ways to do predicates but are more advance topic than the ones mentioned above. The links below talks about them: