tags:

views:

233

answers:

3

I'm having a low-brainwave day... Does anyone know of a quick & elegant way to transform a Dictionary so that the key becomes the value and vice-versa?

Example:

var originalDictionary = new Dictionary<int, string>()
                         { 
                           {1, "One"}, {2, "Two"}, {3, "Three"} 
                         };

becomes

var newDictionary = new Dictionary<string, int>();
// contents:  
// { 
//    {"One", 1}, {"Two", 2}, {"Three", 3} 
// };
+20  A: 

Use ToDictionary ?

orignalDictionary.ToDictionary(kp => kp.Value, kp => kp.Key);

This works because IDictionary<TKey,TElement> is also an IEnumerable<KeyValuePair<TKey,TElement>>. Just be aware that if you have duplicate values, you will get an exception.

In case you have duplicate values, you will need to decide on what to do with them. One simple way would be to ignore duplicates by grouping on Value first, then make the dictionary.

        originalDictionary
            .ToLookup(kp => kp.Value)
            .ToDictionary(g => g.Key, g => g.First());
driis
+1 Great way to handle the duplicate value problem.
Stephan
When I use your example involving `ToLookup`, I get "'System.Linq.IGrouping<MyValueType,KeyValuePair<MyKeyType,MyValueType>>' does not contain a definition for 'Value'". I had to use `ToDictionary` alone on my dictionary.
Sarah Vessels
@Sarah, sounds like you are using ToLookup on an already grouped sequence.
driis
+1  A: 

Here you are:

var reversed = orignalDictionary.ToDictionary(el => el.Value, el => el.Key);

Giorgi
A: 

I agree with the answers provided, however you should consider and make the change in your program to actually set up with the <key, value> instead of making this change after.

VoodooChild
Agreed in principle. But the problem is that the original dictionary serves a specific purpose, and rather than make another soap call, it's more convenient to transform what I have for another library that need the same info but with the key/value reversed.
code4life
I see - good stuff.
VoodooChild