Reputation: 567
I want to filter results According to List of strings, something like this:
List<string> filters = {"a", "b", "c", "d"};
var results = (from R in db.Entries
where R.word.StartsWith(filters[0])
||R.word.StartsWith(filters[1])
||R.word.StartsWith(filters[2])
||...
I don't know the Length of my filters List so how to Query it dynamically in LINQ?
Thanks in advance.
Upvotes: 3
Views: 6103
Reputation: 726489
You should be able to do it like this:
var results = db.Entries
.Where(r => filters.Any(f => r.word.StartsWith(f)));
Extension method Any
is a way to "fold" a chain of ORs ||
applied to a list into a single call.
Upvotes: 6
Reputation: 8920
This works a little bit different in Linq, kind of the other way around
Use the .Contains()
Something like this:
from r in db.entries
where filters.contains (r.word.substring(0,1))
Upvotes: 5