コード例 #1
0
        public static SimpleLookup <TKey, TElement> Create <TSource>(
            IEnumerable <TSource> source,
            Func <TSource, TKey> keySelector,
            Func <TSource, TElement> elementSelector,
            IEqualityComparer <TKey> comparer)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (keySelector == null)
            {
                throw new ArgumentNullException(nameof(keySelector));
            }
            if (elementSelector == null)
            {
                throw new ArgumentNullException(nameof(elementSelector));
            }
            var lookup = new SimpleLookup <TKey, TElement>(comparer);

            foreach (var source1 in source)
            {
                lookup.GetGrouping(keySelector(source1), true).Add(elementSelector(source1));
            }
            return(lookup);
        }
コード例 #2
0
        public static SimpleLookup <TKey, TElement> CreateForJoin(
            IEnumerable <TElement> source,
            Func <TElement, TKey> keySelector,
            IEqualityComparer <TKey> comparer)
        {
            var lookup = new SimpleLookup <TKey, TElement>(comparer);

            foreach (var element in source)
            {
                var key = keySelector(element);
                if (key != null)
                {
                    lookup.GetGrouping(key, true).Add(element);
                }
            }
            return(lookup);
        }