How to convert KeyValuePair to Dictionary as ToDictionary is not available 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.
If I understand correctly you can do it as follows:
new[] { keyValuePair }.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);