Convert List of KeyValuePair into IDictionary "C#"

anbuselvan picture anbuselvan · Oct 26, 2010 · Viewed 24.8k times · Source

My scenario,

how to convert List<KeyValuePair<string, string>> into IDictionary<string, string>?

Answer

Jon Skeet picture Jon Skeet · Oct 26, 2010

Very, very simply with LINQ:

IDictionary<string, string> dictionary =
    list.ToDictionary(pair => pair.Key, pair => pair.Value);

Note that this will fail if there are any duplicate keys - I assume that's okay?