Class EnumerableExtensions
- Namespace
- Cuemon.Extensions.Collections.Generic
- Assembly
- Cuemon.Extensions.Collections.Generic.dll
Extension methods for the IEnumerable<T> interface.
public static class EnumerableExtensions
- Inheritance
-
EnumerableExtensions
Methods
Chunk<T>(IEnumerable<T>, int)
Returns a chunked IEnumerable<T> sequence with a maximum of the specified size. Default is 128.
public static PartitionerEnumerable<T> Chunk<T>(this IEnumerable<T> source, int size = 128)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
sizeintThe amount of elements to process at a time.
Returns
- PartitionerEnumerable<T>
An IEnumerable<T> that contains no more than the specified
sizeof elements from thesourcesequence.
Type Parameters
TThe type of the elements of
source.
Remarks
The original source is reduced equivalent to the number of elements in the returned sequence.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sizeis less or equal to 0.
OrderAscending<T>(IEnumerable<T>)
Returns ascending sorted elements from a sequence by using the default comparer to compare values.
public static IEnumerable<T> OrderAscending<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains ascending sorted elements from the source sequence.
Type Parameters
TThe type of the elements of
source.
OrderAscending<T>(IEnumerable<T>, IComparer<T>)
Returns ascending sorted elements from a sequence by using a specified IComparer<T> to compare values.
public static IEnumerable<T> OrderAscending<T>(this IEnumerable<T> source, IComparer<T> comparer)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
comparerIComparer<T>An IComparer<T> to compare values.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains ascending sorted elements from the source sequence.
Type Parameters
TThe type of the elements of
source.
OrderDescending<T>(IEnumerable<T>)
Returns descending sorted elements from a sequence by using the default comparer to compare values.
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains descending sorted elements from the source sequence.
Type Parameters
TThe type of the elements of
source.
OrderDescending<T>(IEnumerable<T>, IComparer<T>)
Returns descending sorted elements from a sequence by using a specified IComparer<T> to compare values.
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> source, IComparer<T> comparer)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
comparerIComparer<T>An IComparer<T> to compare values.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains descending sorted elements from the source sequence.
Type Parameters
TThe type of the elements of
source.
RandomOrDefault<T>(IEnumerable<T>)
Returns a random element of a sequence of elements, or a default value if no element is found.
public static T RandomOrDefault<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
Returns
- T
defaultifsourceis empty; otherwise, a random element ofsource.
Type Parameters
TThe type of the elements of
source.
Shuffle<T>(IEnumerable<T>)
Shuffles the specified source like a deck of cards.
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
A sequence of
Twith the shuffledsource.
Type Parameters
T
Remarks
Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Shuffle<T>(IEnumerable<T>, Func<int, int, int>)
Shuffles the specified source like a deck of cards.
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Func<int, int, int> randomizer)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
randomizerFunc<int, int, int>The function delegate that will handle the randomization of
source.
Returns
- IEnumerable<T>
A sequence of
Twith the shuffledsource.
Type Parameters
T
Remarks
Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)
Creates a Dictionary<TKey, TValue> from the specified source sequence.
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
Parameters
sourceIEnumerable<KeyValuePair<TKey, TValue>>An IEnumerable<T> to extend.
Returns
- IDictionary<TKey, TValue>
A Dictionary<TKey, TValue> that is equivalent to the specified
sourcesequence.
Type Parameters
TKeyThe type of keys in the
source.TValueThe type of values in the
source.
Exceptions
- ArgumentNullException
sourceis null.- ArgumentException
sourcecontains at least one KeyValuePair<TKey, TValue> that produces duplicate keys for two elements.
ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>)
Creates a Dictionary<TKey, TValue> from the specified source sequence.
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
Parameters
sourceIEnumerable<KeyValuePair<TKey, TValue>>An IEnumerable<T> to extend.
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
Returns
- IDictionary<TKey, TValue>
A Dictionary<TKey, TValue> that is equivalent to the specified
sourcesequence.
Type Parameters
TKeyThe type of keys in the
source.TValueThe type of values in the
source.
Exceptions
- ArgumentNullException
sourceis null - or -compareris null.- ArgumentException
sourcecontains at least one KeyValuePair<TKey, TValue> that produces duplicate keys for two elements.
ToPaginationList<T>(IEnumerable<T>, Func<int>, Action<PaginationOptions>)
Converts the specified source to an eagerly materialized generic and read-only pagination list.
public static PaginationList<T> ToPaginationList<T>(this IEnumerable<T> source, Func<int> totalElementCounter, Action<PaginationOptions> setup = null)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
totalElementCounterFunc<int>The total element counter.
setupAction<PaginationOptions>The PaginationOptions which may be configured.
Returns
- PaginationList<T>
An instance of PaginationList<T>.
Type Parameters
TThe type of elements in the collection.
ToPagination<T>(IEnumerable<T>, Func<int>, Action<PaginationOptions>)
Converts the specified source to a generic and read-only pagination sequence.
public static PaginationEnumerable<T> ToPagination<T>(this IEnumerable<T> source, Func<int> totalElementCounter, Action<PaginationOptions> setup = null)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
totalElementCounterFunc<int>The total element counter.
setupAction<PaginationOptions>The PaginationOptions which may be configured.
Returns
- PaginationEnumerable<T>
An instance of PaginationEnumerable<T>.
Type Parameters
TThe type of elements in the collection.
ToPartitioner<T>(IEnumerable<T>, int)
Extends the specified source to support iterating in partitions.
public static PartitionerEnumerable<T> ToPartitioner<T>(this IEnumerable<T> source, int partitionSize = 128)
Parameters
sourceIEnumerable<T>An IEnumerable<T> to extend.
partitionSizeintThe size of the partitions.
Returns
- PartitionerEnumerable<T>
An instance of PartitionerEnumerable<T>.
Type Parameters
TThe type of elements in the
source.
Yield<T>(T)
Returns an IEnumerable<T> sequence with the specified value as the only element.
public static IEnumerable<T> Yield<T>(this T value)
Parameters
valueTThe
Tto extend.
Returns
- IEnumerable<T>
An IEnumerable<T> sequence with the specified
valueas the only element.
Type Parameters
TThe type of the element of IEnumerable<T>.