zsharp
zsharp

Reputation: 13766

Filtering a List with another List Inclusive

i have a list of filter names: FILTERBYNAMES

my query result items each contain a name list: NAMES

I want to filter the result an take all items whose name list contains at least one name in the FILTERNAMELIST:

   results= result.where(r=>r.NAMES.CONTAINS(...?)...?

Upvotes: 1

Views: 375

Answers (3)

Amy B
Amy B

Reputation: 110221

To limit the enumerations of the filter, you could use a hashset...

HashSet<string> hashedFilter = new HashSet<string>(filterByNames);

var results = result
  .Where(x => x.Names
    .Any(name => hashedFilter.Contains(name))
  );

Upvotes: 1

Daniel Br&#252;ckner
Daniel Br&#252;ckner

Reputation: 59705

You can solve this by looking at the intersection of the two name sets.

var filteredResult = result.Where(i => i.Names.Intersect(filter).Any());

Upvotes: 4

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422280

I think you need something like:

var results = list.Where(i => i.Names
                               .Any(name => filterNameList.Contains(name)));

Upvotes: 5

Related Questions