Is there streamlined way to convert list/enumberable of KeyValuePair<T, U>
to Dictionary<T, U>
?
Linq transformation, .ToDictionary() extension did not work.
.ToDictionary(kvp=>kvp.Key,kvp=>kvp.Value);
Isn't that much more work.
You can create your own extension method that would perform as you expect.
public static class KeyValuePairEnumerableExtensions
{
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
return source.ToDictionary(item => item.Key, item => item.Value);
}
}
This is the best I could produce:
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> keyValuePairs)
{
var dict = new Dictionary<TKey, TValue>();
var dictAsIDictionary = (IDictionary<TKey, TValue>) dict;
foreach (var property in keyValuePairs)
{
(dictAsIDictionary).Add(property);
}
return dict;
}
I compared the speed of converting an IEnumerable of 20 million key value pairs to a Dictionary using Linq.ToDictionary with the speed of this one. This one ran in 80% of the time of the Linq version. So it's faster, but not a lot. I think you'd really need to value that 20% saving to make it worth using.
Similar to the others, but using new
instead of ToDictionary
(since new
already supports KeyValuePair
enumerations) and allowing the passing of an IEqualityComparer<TKey>
.
Also including a ToReadOnlyDictionary
variant for completeness.
public static class EnumerableKeyValuePairExtensions {
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> keyValuePairs, IEqualityComparer<TKey>? comparer = null)
where TKey : notnull
=> new Dictionary<TKey, TValue>(keyValuePairs, comparer);
public static ReadOnlyDictionary<TKey, TValue> ToReadOnlyDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> keyValuePairs, IEqualityComparer<TKey>? comparer = null)
where TKey : notnull
=> new ReadOnlyDictionary<TKey, TValue>(keyValuePairs.ToDictionary(comparer));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With