public static Dictionary <TKey, TElement> ToDictionary <T, TKey, TElement>(
            [InstantHandle] this IEnumerable <T> source,
            [InstantHandle] Func <T, TKey> keySelector,
            [InstantHandle] Func <T, TElement> elementSelector,
            IEqualityComparer <TKey>?comparer,
            DictionaryDuplicate duplicateHandling)
            where TKey : notnull
        {
            Code.NotNull(source, nameof(source));
            Code.NotNull(keySelector, nameof(keySelector));
            Code.NotNull(elementSelector, nameof(elementSelector));
            Code.InRange(
                (int)duplicateHandling, nameof(duplicateHandling), (int)DictionaryDuplicate.Throw, (int)DictionaryDuplicate.LastWins);

            if (duplicateHandling == DictionaryDuplicate.Throw)
            {
                return(source.ToDictionary(keySelector, elementSelector, comparer));
            }

            var result = new Dictionary <TKey, TElement>(comparer);

            foreach (var item in source)
            {
                var key = keySelector(item);
                if (duplicateHandling == DictionaryDuplicate.LastWins || !result.ContainsKey(key))
                {
                    result[key] = elementSelector(item);
                }
            }

            return(result);
        }
 public static Dictionary <TKey, TElement> ToDictionary <T, TKey, TElement>(
     [InstantHandle] this IEnumerable <T> source,
     [InstantHandle] Func <T, TKey> keySelector,
     [InstantHandle] Func <T, TElement> elementSelector,
     DictionaryDuplicate duplicateHandling)
     where TKey : notnull =>
 ToDictionary(source, keySelector, elementSelector, null, duplicateHandling);
 public static Dictionary <TKey, T> ToDictionary <T, TKey>(
     [InstantHandle] this IEnumerable <T> source,
     [InstantHandle] Func <T, TKey> keySelector,
     IEqualityComparer <TKey>?comparer,
     DictionaryDuplicate duplicateHandling)
     where TKey : notnull =>
 ToDictionary(source, keySelector, Fn <T> .Self, comparer, duplicateHandling);
 public static Dictionary <TKey, T> ToDictionary <T, TKey>(
     [NotNull, InstantHandle] this IEnumerable <T> source,
     [NotNull, InstantHandle] Func <T, TKey> keySelector,
     [CanBeNull] IEqualityComparer <TKey> comparer,
     DictionaryDuplicate duplicateHandling) =>
 ToDictionary(source, keySelector, Fn <T> .Self, comparer, duplicateHandling);
 public static Dictionary <TKey, T> ToDictionary <T, TKey>(
     [NotNull, InstantHandle] this IEnumerable <T> source,
     [NotNull, InstantHandle] Func <T, TKey> keySelector,
     DictionaryDuplicate duplicateHandling) =>
 ToDictionary(source, keySelector, Fn <T> .Self, null, duplicateHandling);
Пример #6
0
 public static Dictionary <TKey, TElement> ToDictionary <T, TKey, TElement>(
     [NotNull] this IEnumerable <T> source,
     [NotNull] Func <T, TKey> keySelector,
     [NotNull] Func <T, TElement> elementSelector,
     DictionaryDuplicate duplicateHandling) =>
 ToDictionary(source, keySelector, elementSelector, null, duplicateHandling);