Reputation: 611
I have a List<string> filterCriteria
which contains one or multiple keywords to search a column in my database with based on user selection on the client side.
The problem i have is that im not quite sure on how to build my linq to sql statement as it could have no 'or' operator or it could have multiple (10+).
This is my original query
var originalQuery = (from p in productContext.Products
select p);
Then based on the list i need to query 'originalQuery' by the words in my List<string> filterCriteria
using or operators.
E.g.
originalQuery = originalQuery.Where(p => p.ProductRange == "criteria1" ||
p.ProductRange == "criteria2");
And so on...
Upvotes: 1
Views: 234
Reputation: 7545
Yet another way of writing the same thing as Martin:
var originalQuery =
from p in productContext.Products
where filterCriteria.Contains(p.ProductRange)
select p;
Upvotes: 1
Reputation: 75306
Another way you can use Any
:
originalQuery.Where(p => filterCriteria.Any(c => c == p.ProductRange));
Upvotes: 1
Reputation: 7332
You could do it like this:
originalQuery = originalQuery.Where(p => filterCriteria.Contains(p.ProductRange));
This way, you will get all items in the originalQuery which have one of the selected values for ProductRange.
Upvotes: 2