/// <summary> /// Returns distinct elements from a sequence by using a specified delegates to compare values. /// </summary> /// <typeparam name="T">The type of the elements of source.</typeparam> /// <param name="source">The sequence to remove duplicate elements from</param> /// <param name="equalityComparer">A delegate to compare if two elements are equal.</param> /// <param name="hasher">A delegate tp calculate an element hash.</param> /// <returns>An IEnumerable<T> that contains distinct elements from the source sequence.</returns> public static IEnumerable <T> Distinct <T>( this IEnumerable <T> source, EqualityComparerDelegate <T> equalityComparer, HashDelegate <T> hasher = null) { return(source.Distinct(new DelegateEqualityComparer <T>(equalityComparer, hasher))); }
/// <summary> /// Produces the set difference of two sequences by using the specified delegates to compare values. /// </summary> /// <typeparam name="T">The type of the elements of the input sequences.</typeparam> /// <param name="first">An IEnumerable<T> whose elements that are not also insecond will be returned.</param> /// <param name="second"> /// An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence. /// </param> /// <param name="equalityComparer">A delegate to compare if two elements are equal.</param> /// <param name="hasher">A delegate tp calculate an element hash.</param> /// <returns>A sequence that contains the set difference of the elements of two sequences.</returns> public static IEnumerable <T> Except <T>( this IEnumerable <T> first, IEnumerable <T> second, EqualityComparerDelegate <T> equalityComparer, HashDelegate <T> hasher = null) { return(first.Except(second, new DelegateEqualityComparer <T>(equalityComparer, hasher))); }
/// <summary> /// Produces the set union of two sequences by using a specified comparison delegates. /// </summary> /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam> /// <param name="first">An IEnumerable<T> whose distinct elements form the first set for the union.</param> /// <param name="second">An IEnumerable<T> whose distinct elements form the second set for the union.</param> /// <param name="equalityComparer">A delegate to compare if two elements are equal.</param> /// <param name="hasher">A delegate tp calculate an element hash.</param> /// <returns>An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.</returns> public static IEnumerable <TSource> Union <TSource>( this IEnumerable <TSource> first, IEnumerable <TSource> second, EqualityComparerDelegate <TSource> equalityComparer, HashDelegate <TSource> hasher) { return(first.Union(second, new DelegateEqualityComparer <TSource>(equalityComparer, hasher))); }
/// <summary> /// Creates a Lookup<TKey, TElement> from an IEnumerable<T> according to a specified key selector function and key comparison delegates. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam> /// <param name="source">The IEnumerable<T> to create a Lookup<TKey, TElement> from.</param> /// <param name="keySelector">A function to extract a key from each element.</param> /// <param name="keyComparer">A delegate to compare if two keys are equal.</param> /// <param name="keyHasher">A delegate tp calculate a key hash.</param> /// <returns>A Lookup<TKey, TElement> that contains keys and values.</returns> public static ILookup <TKey, TSource> ToLookup <TSource, TKey>( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, EqualityComparerDelegate <TKey> keyComparer, HashDelegate <TKey> keyHasher = null) { return(source.ToLookup(keySelector, new DelegateEqualityComparer <TKey>(keyComparer, keyHasher))); }
/// <summary> /// Determines whether two sequences are equal by comparing their elements by using the specified delegates. /// </summary> /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam> /// <param name="first">An IEnumerable<T> to compare tosecond.</param> /// <param name="second">An IEnumerable<T> to compare to the first sequence.</param> /// <param name="equalityComparer">A delegate to compare if two elements are equal.</param> /// <param name="hasher">A delegate tp calculate an element hash.</param> /// <returns>true if the two source sequences are of equal length and their corresponding elements compare equal according to comparer; otherwise, false.</returns> public static bool SequenceEqual <TSource>( this IEnumerable <TSource> first, IEnumerable <TSource> second, EqualityComparerDelegate <TSource> equalityComparer, HashDelegate <TSource> hasher = null) { return(first.SequenceEqual(second, new DelegateEqualityComparer <TSource>(equalityComparer, hasher))); }
/// <summary> /// Creates a Dictionary<TKey, TValue> from an IEnumerable<T> according to a specified key selector function, a comparison delegates, and an element selector function. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam> /// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam> /// <param name="source">An IEnumerable<T> to create a Dictionary<TKey, TValue> from.</param> /// <param name="keySelector">A function to extract a key from each element.</param> /// <param name="elementSelector">A transform function to produce a result element value from each element.</param> /// <param name="keyComparer">A delegate to compare if two keys are equal.</param> /// <param name="keyHasher">A delegate tp calculate a key hash.</param> /// <returns>A Dictionary<TKey, TValue> that contains values of type TElement selected from the input sequence.</returns> public static Dictionary <TKey, TElement> ToDictionary <TSource, TKey, TElement>( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector, EqualityComparerDelegate <TKey> keyComparer, HashDelegate <TKey> keyHasher = null) { return(source.ToDictionary(keySelector, elementSelector, new DelegateEqualityComparer <TKey>(keyComparer, keyHasher))); }
/// <summary> /// Produces the set intersection of two sequences by using the specified delegates to compare values. /// </summary> /// <typeparam name="TSource">The type of the elements of the input sequences.</typeparam> /// <param name="first">An IEnumerable<T> whose distinct elements that also appear insecond will be returned.</param> /// <param name="second">An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.</param> /// <param name="equalityComparer">A delegate to compare if two elements are equal.</param> /// <param name="hasher">A delegate tp calculate an element hash.</param> /// <returns>A sequence that contains the elements that form the set intersection of two sequences.</returns> public static IEnumerable <TSource> Intersect <TSource>( this IEnumerable <TSource> first, IEnumerable <TSource> second, EqualityComparerDelegate <TSource> equalityComparer, HashDelegate <TSource> hasher = null) { return(first.Intersect( second, new DelegateEqualityComparer <TSource>(equalityComparer, hasher))); }
/// <summary> /// Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. /// Key values are compared by using a specified delegates, and the elements of each group are projected by using a specified function. /// </summary> /// <typeparam name="TSource">The type of the elements of source.</typeparam> /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam> /// <typeparam name="TElement">The type of the elements in each IGrouping<TKey, TElement>.</typeparam> /// <typeparam name="TResult">The type of the result value returned by resultSelector.</typeparam> /// <param name="source">An IEnumerable<T> whose elements to group.</param> /// <param name="keySelector">A function to extract the key for each element.</param> /// <param name="elementSelector">A function to map each source element to an element in an IGrouping<TKey, TElement>.</param> /// <param name="resultSelector">A function to create a result value from each group.</param> /// <param name="keyComparer">A delegate to compare if two keys are equal.</param> /// <param name="keyHasher">A delegate tp calculate a key hash.</param> /// <returns>A collection of elements of type TResult where each element represents a projection over a group and its key.</returns> public static IEnumerable <TResult> GroupBy <TSource, TKey, TElement, TResult>( this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector, Func <TKey, IEnumerable <TElement>, TResult> resultSelector, EqualityComparerDelegate <TKey> keyComparer, HashDelegate <TKey> keyHasher = null) { return(source.GroupBy(keySelector, elementSelector, resultSelector, new DelegateEqualityComparer <TKey>(keyComparer, keyHasher))); }
/// <summary> /// Creates new instance of the class. /// </summary> /// <param name="equalityComparer">A delegate to compare the equality of two objects.</param> /// <param name="hasher">A delegate to calculate hash for a given object.</param> public DelegateEqualityComparer(EqualityComparerDelegate <T> equalityComparer, HashDelegate <T> hasher = null) { if (equalityComparer == null) { throw new ArgumentNullException("equalityComparer"); } if (hasher == null) { hasher = obj => obj.GetHashCode(); } this.hasher = hasher; this.equalityComparer = equalityComparer; }
/// <summary> /// Correlates the elements of two sequences based on matching keys. Specified delegate are used to compare keys. /// </summary> /// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam> /// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam> /// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam> /// <typeparam name="TResult">The type of the result elements.</typeparam> /// <param name="outer">The first sequence to join.</param> /// <param name="inner">The sequence to join to the first sequence.</param> /// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param> /// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param> /// <param name="resultSelector">A function to create a result element from two matching elements.</param> /// <param name="keyComparer">A delegate to compare if two keys are equal.</param> /// <param name="keyHasher">A delegate tp calculate a key hash.</param> /// <returns>An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two sequences.</returns> public static IEnumerable <TResult> Join <TOuter, TInner, TKey, TResult>( this IEnumerable <TOuter> outer, IEnumerable <TInner> inner, Func <TOuter, TKey> outerKeySelector, Func <TInner, TKey> innerKeySelector, Func <TOuter, TInner, TResult> resultSelector, EqualityComparerDelegate <TKey> keyComparer, HashDelegate <TKey> keyHasher = null) { return(outer.Join( inner, outerKeySelector, innerKeySelector, resultSelector, new DelegateEqualityComparer <TKey>(keyComparer, keyHasher))); }