Skip to content

Added two 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.

Notifications You must be signed in to change notification settings

Kkamikadzee/DotNetDistinctByExtension

Repository files navigation

DotNetDistinctByExtension

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)

Type Parameters

TSource

The type of elements of source.

TKey

The type of key to distinguish elements by.

TSort

The type of key to maximizing elements by.

Parameters

source

IEnumerable<TSource>

The sequence to remove duplicate elements from.

keySelector

Func<TSource,TKey>

A function to extract the key to distinguish for each element.

keySort

Func<TSource,TSort>

A function to extract the key maximizing for each element.

keySortComparer

IComparer<TSort>

An IComparer to compare maximizing keys.

duplicateComparer

IEqualityComparer<TKey>

An IEqualityComparer to compare maximizing keys.

Returns

IEnumerable<TSource>

An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.

Exceptions

ArgumentNullException

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)

Type Parameters

TSource

The type of elements of source.

TKey

The type of key to distinguish elements by.

TSort

The type of key to maximizing elements by.

Parameters

source

IEnumerable<TSource>

The sequence to remove duplicate elements from.

keySelector

Func<TSource,TKey>

A function to extract the key to distinguish for each element.

keySort

Func<TSource,TSort>

A function to extract the key maximizing for each element.

keySelectorComparer

IComparer<TKey>

An IComparer to compare distinguish keys.

keySortComparer

IComparer<TSort>

An IComparer to compare maximizing keys.

duplicateComparer

IEqualityComparer<TKey>

An IEqualityComparer to compare maximizing keys.

Returns

IEnumerable<TSource>

An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.

Exceptions

ArgumentNullException

source is null.

Remarks

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)

Type Parameters

TSource

The type of elements of source.

TKey

The type of key to distinguish elements by.

TSort

The type of key to maximizing elements by.

Parameters

source

IEnumerable<TSource>

The sequence to remove duplicate elements from.

keySelector

Func<TSource,TKey>

A function to extract the key to distinguish for each element.

keySort

Func<TSource,TSort>

A function to extract the key maximizing for each element.

keySortComparer

IComparer<TSort>

An IComparer to compare maximizing keys.

duplicateComparer

IEqualityComparer<TKey>

An IEqualityComparer to compare maximizing keys.

Returns

IEnumerable<TSource>

An IEnumerable that contains distinct elements from the source sequence, with the elements having the maximum TSort value.

Exceptions

ArgumentNullException

source is null.

Benchmark result

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.

About

Added two 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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages