nik
nik

Reputation: 1784

How to filter a list for multiple startswith criteria?

If I filter a list for multiple criteria I can do this like this:

string[] criteria = { "a1", "b1" };
var res = reslist.Where(x => criteria.Contains(x.book)).ToList();

Now I would like to do sth like this:

string[] criteria = { "a", "b" };
var res = reslist.Where(x => criteria.ContainsStartsWith(x.book)).ToList();

Obviously this doesnt exists. How can I check through a criteria list with startswith values?

Upvotes: 0

Views: 359

Answers (2)

Ankit
Ankit

Reputation: 790

Minor add to Hari's answer

Use below

var res = reslist.Where(x => criteria.Any(s=>s.StartsWith(x.book) || criteria.Contains(x.book)).ToList();

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

You could use Any extension method .

var res = reslist.Where(x => criteria.Any(s=>s.StartsWith(x.book)).ToList();

Upvotes: 1

Related Questions