Of course, use a constructor that accepts IEnumerable<KeyValuePair<int,Person>> :
var personDictionary = new ConcurrentDictionary<int, Person> (PersonArray.ToDictionary(person => person.Age));
var points to ConcurrentDictionary<int,Person> .
If you intend to create an extension method, as Wasp suggested, I would recommend using the following version, which offers a slightly smoother syntax:
public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue> (this IEnumerable<TValue> source, Func<TValue, TKey> valueSelector) { return new ConcurrentDictionary<TKey, TValue> (source.ToDictionary(valueSelector)); }
Usage is similar to ToDictionary , creating a constant feel:
var dict = PersonArray.ToConcurrentDictionary(person => person.Age);
Adam source share