public static class EnumerableExtensions
        /// <summary>
        /// Creates a <see cref="T:System.Collections.Generic.Dictionary`2"/> from an 
        /// <see cref="T:System.Collections.Generic.IEnumerable`1"/> according to a specified 
        /// key selector function, and an element selector function.
        /// </summary>
        public static IDictionary<TKey, TElement> ToDistinctDictionary<TSource, TKey, TElement>(
            this IEnumerable<TSource> source,
            Func<TSource, TKey> keySelector,
            Func<TSource, TElement> elementSelector)
            if (source == null) throw new NullReferenceException("The 'source' cannot be null.");
            if (keySelector == null) throw new ArgumentNullException("keySelector");
            if (elementSelector == null) throw new ArgumentNullException("elementSelector");

            var dictionary = new Dictionary<TKey, TElement>();
            foreach (TSource current in source)
                dictionary[keySelector(current)] = elementSelector(current);
            return dictionary;
var dictionary = someList.ToDistinctDictionary(li => li.Key, li => li);


Creates an IDictionary&lt;TKey, TValue&gt; from the IEnumerable&lt;TSource&gt; instance based on the key selector and element selector. This is distinct by using the built-in index of the dictionary instance for either adding or updating a keys corresponding value.


Double click on the code to select all.