LINQ transform Dictionary<key,value> to Dictionary<value,key>

Posted by code4life on Stack Overflow See other posts from Stack Overflow or by code4life
Published on 2010-06-03T17:31:56Z Indexed on 2010/06/03 17:44 UTC
Read the original article Hit count: 234

Filed under:
|

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} 
// };

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ