Exemple #1
0
        /// <summary>
        /// Creates a KeyEqualityComparer based on the current key comparer that
        /// in addition checks for equality based the value returned by the key selector.
        /// </summary>
        /// <typeparam name="TKey">The type of the value returned by the key selector.</typeparam>
        /// <param name="keySelector">The delegate used to select the next key.</param>
        /// <returns>
        /// A KeyEqualityComparer based on the current key comparer that
        /// in addition checks for equality based the value returned by the key selector.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">The key selector is null.</exception>
        public KeyEqualityComparer <T> And <TKey>(Func <T, TKey> keySelector)
        {
            if (keySelector == null)
            {
                throw new ArgumentNullException(nameof(keySelector));
            }
            var comparer = new CompoundKeyEqualityComparer <T>();

            comparer.Add(this);
            comparer.Add(new TypedKeyEqualityComparer <T, TKey>(keySelector, EqualityComparer <TKey> .Default));
            return(comparer);
        }
        public void Add(KeyEqualityComparer <T> comparer)
        {
            CompoundKeyEqualityComparer <T> compoundComparer = comparer as CompoundKeyEqualityComparer <T>;

            if (compoundComparer == null)
            {
                _comparers.Add(comparer);
            }
            else
            {
                _comparers.AddRange(compoundComparer._comparers);
            }
        }
        /// <summary>
        /// Creates a KeyEqualityComparer based on the current key comparer that
        /// in addition checks for equality based the value returned by the key selector.
        /// </summary>
        /// <typeparam name="TKey">The type of the value returned by the key selector.</typeparam>
        /// <param name="keySelector">The delegate used to select the next key.</param>
        /// <param name="keyComparer">The equality comparer to use to compare the keys.</param>
        /// <returns>
        /// A KeyEqualityComparer based on the current key comparer that
        /// in addition checks for equality based the value returned by the key selector.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">The key selector is null.</exception>
        /// <exception cref="System.ArgumentNullException">The key comparer is null.</exception>
        public KeyEqualityComparer <T> And <TKey>(Func <T, TKey> keySelector, IEqualityComparer <TKey> keyComparer)
        {
            if (keySelector == null)
            {
                throw new ArgumentNullException("keySelector");
            }
            if (keyComparer == null)
            {
                throw new ArgumentNullException("keyComparer");
            }
            CompoundKeyEqualityComparer <T> comparer = new CompoundKeyEqualityComparer <T>();

            comparer.Add(this);
            comparer.Add(new TypedKeyEqualityComparer <T, TKey>(keySelector, keyComparer));
            return(comparer);
        }