Added three IEnumerable<T> extension that returns sorted distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys, with the remaining elements having the maximum keySort value.
DistinctByWithMax<TSource, TKey, TSort>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, IComparer<TSort>? keySortComparer = null, IEqualityComparer<TKey>? duplicateComparer = null)
Returns sorted distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys, with the remaining elements having the maximum keySort value. Facade containing a linq query.
Description
public static System.Collections.Generic.IEnumerable<TSource> DistinctByWithMax<TSource, TKey, TSort>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, System.Collections.Generic.IComparer<TSort>? keySortComparer = null, System.Collections.Generic.IEqualityComparer<TKey>? duplicateComparer = null)
The type of elements of source.
The type of key to distinguish elements by.
The type of key to maximizing elements by.
The sequence to remove duplicate elements from.
A function to extract the key to distinguish for each element.
A function to extract the key maximizing for each element.
An IComparer to compare maximizing keys.
An IEqualityComparer to compare maximizing keys.
An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.
source is null.
DistinctByWithMaxSorted<TSource, TKey, TSort>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, IComparer<TKey>? keySelectorComparer = null, IComparer<TSort>? keySortComparer = null, IEqualityComparer<TKey>? duplicateComparer = null)
Returns sorted distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys, with the remaining elements having the maximum keySort value.
Description
public static System.Collections.Generic.IEnumerable<TSource> DistinctByWithMaxSorted<TSource, TKey, TSort>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, System.Collections.Generic.IComparer<TKey>? keySelectorComparer = null, System.Collections.Generic.IComparer<TSort>? keySortComparer = null, System.Collections.Generic.IEqualityComparer<TKey>? duplicateComparer = null)
The type of elements of source.
The type of key to distinguish elements by.
The type of key to maximizing elements by.
The sequence to remove duplicate elements from.
A function to extract the key to distinguish for each element.
A function to extract the key maximizing for each element.
An IComparer to compare distinguish keys.
An IComparer to compare maximizing keys.
An IEqualityComparer to compare maximizing keys.
An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.
source is null.
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C#.
DistinctByWithMaxHash<TSource, TKey, TSort>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, IComparer<TSort>? keySortComparer = null, IEqualityComparer<TKey>? duplicateComparer = null)
Returns sorted distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys, with the remaining elements having the maximum keySort value. Explicitly uses a dictionary to find duplicates.
Description
public static System.Collections.Generic.IEnumerable<TSource> DistinctByWithMaxHash<TSource, TKey, TSort>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TSort> keySort, System.Collections.Generic.IComparer<TSort>? keySortComparer = null, System.Collections.Generic.IEqualityComparer<TKey>? duplicateComparer = null)
The type of elements of source.
The type of key to distinguish elements by.
The type of key to maximizing elements by.
The sequence to remove duplicate elements from.
A function to extract the key to distinguish for each element.
A function to extract the key maximizing for each element.
An IComparer to compare maximizing keys.
An IEqualityComparer to compare maximizing keys.
An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.
source is null.
Method | Mean | Error | StdDev |
---|---|---|---|
Linq | 6.762 ms | 0.0355 ms | 0.0332 ms |
Sort | 25.206 ms | 0.1311 ms | 0.1095 ms |
Hash | 6.924 ms | 0.0052 ms | 0.0043 ms |
Расширения сделаны в развлекательных целях.
Пример задачи, которую помогает решить один из вышеописанных методов: Дана коллекия объектов класса FooBar с полями Foo, Bar и Baz. Необходимо получить коллекцию, которая не будет содержать объект с одинаковыми полями Foo и Bar. При этом, если в исходной коллекции хранится два объекта с одинаковыми Foo и Bar и разными Baz, в результирующей коллекции должен остаться объект с бóльшим Baz.