Returns a dictionary with keyvalue pairs corresponding to items from the given sequence. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Returns a dictionary with keyvalue pairs corresponding to items from the given sequence.

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;// ww w .  j  a v  a 2  s.c o  m

public class Main{
        /// <summary>
        /// Returns a dictionary with key/value pairs corresponding to items from the given sequence.
        /// The key used for each item is the result of applying the given key selector function to the item.
        /// The value used for each item is the item itself.
        /// </summary>
        public static IReadOnlyDictionary<TKey, TValue> KeyedBy<TKey, TValue>(this IEnumerable<TValue> sequence, Func<TValue, TKey> keySelector) {
            return sequence.ToDictionary(keySelector, e => e);
        }
}

Related Tutorials