Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: Getting Keys for a given list of Values from Dictionary and vice versa

Tags:

I have the following structure in my code Dictionary<TKeys, TValues> data;. I run some LINQ queries on both data types and often need to switch between Keys and Values. What is the best way to get the list of Keys for given Values and vice versa? Please, notice, that I usually have 'IEnumerable' and 'IEnumerable' as a result of my previous LINQ queries and would like to have something like IEnumerable<TKeys> Dictionary.GetAllKeys(IEnumerable<IValues> vals) and IEnumerable<TValues> Dictionary.GetAllValues(IEnumerable<IKeys> keys).

Maybe I need other data container for this task?

Regards, Alexander.

like image 626
Alexander Galkin Avatar asked Jun 27 '11 19:06

Alexander Galkin


1 Answers

 var values = dictionary.Where(x => someKeys.Contains(x.Key)).Select(x => x.Value);  var keys = dictionary.Where(x => someValues.Contains(x.Value)).Select(x => x.Key); 
like image 137
Bala R Avatar answered Apr 28 '23 02:04

Bala R