How to Convert KeyValuePair to Dictionary in C#

var dictionary = new Dictionary<string, object> { { kvp.Key, kvp.Value } };

ToDictionary does exist in C# (edit: not the same ToDictionary you were thinking of) and can be used like this:

var list = new List<KeyValuePair<string, object>>{kvp};
var dictionary = list.ToDictionary(x => x.Key, x => x.Value);

Here list could be a List or other IEnumerable of anything. The first lambda shows how to extract the key from a list item, and the second shows how to extract the value. In this case they are both trivial.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)