Is there an elegant way to map one Dictionary to another using Linq in the .NET framework?
This can be accomplished via enumerating with foreach:
var d1 = new Dictionary<string, string>() {
{ "One", "1" },
{ "Two", "2" }
};
// map dictionary 1 to dictionary 2 without LINQ
var d2 = new Dictionary<string, int>();
foreach(var kvp in d1) {
d2.Add(kvp.Value, int.Parse(kvp.Value));
}
...but I'm looking for some way to accomplish with LINQ:
// DOES NOT WORK
Dictionary<string, int> d2 =
d1.Select(kvp => {
return new KeyValuePair<string, int>(kvp.Key, int.Parse(kvp.Value));
})