Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have a ContainsAny() method for a dictionary?

I have a dictionary where the Key is an Id and the value is a string

I now have a List of person objects where each person has a CarIds property that is an

IEnumerable<int>

I want to basically filter the list of objects to only include items where one of the properties is included in the dict.

For example. something like this:

var dictionary = GetDict();

var people = GetPeople();

 people = people.Where(r => dictionary.ContainsAny(r.CarIds)).ToList();

Does something like this exist where I can do something similar to ContainsKey() but check for any in a list of ints ?

like image 362
leora Avatar asked Apr 22 '13 03:04

leora


1 Answers

Yes, you can try this for lists:

people = people.Where(r => list.Intersect(r.CarIds).Count() != 0).ToList();

For dictionaries, you can use this:

people = people.Where(r => r.CarIds.Any(n => dictionary.ContainsKey(n))).ToList();
like image 197
Sergey Kalinichenko Avatar answered Nov 10 '22 14:11

Sergey Kalinichenko