Class EnumerableExtensions
- Assembly
- Cuemon.Extensions.Collections.Generic.dll
Extension methods for the IEnumerable<T> interface.
public static class EnumerableExtensions
- Inheritance
-
EnumerableExtensions
Methods
Chunk<T>(IEnumerable<T>, Int32)
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
source
IEnumerable<T>An IEnumerable<T> to extend.
size
Int32The amount of elements to process at a time.
Returns
- PartitionerEnumerable<T>
An IEnumerable<T> that contains no more than the specified
size
of elements from thesource
sequence.
Type Parameters
T
The type of the elements of
source
.
Remarks
The original source
is reduced equivalent to the number of elements in the returned sequence.
Exceptions
- ArgumentNullException
source
is null.- ArgumentException
size
is 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
source
IEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains ascending sorted elements from the source sequence.
Type Parameters
T
The 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
source
IEnumerable<T>An IEnumerable<T> to extend.
comparer
IComparer<T>An IComparer<T> to compare values.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains ascending sorted elements from the source sequence.
Type Parameters
T
The 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
source
IEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains descending sorted elements from the source sequence.
Type Parameters
T
The 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
source
IEnumerable<T>An IEnumerable<T> to extend.
comparer
IComparer<T>An IComparer<T> to compare values.
Returns
- IEnumerable<T>
An IEnumerable<T> that contains descending sorted elements from the source sequence.
Type Parameters
T
The 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
source
IEnumerable<T>An IEnumerable<T> to extend.
Returns
- T
default
ifsource
is empty; otherwise, a random element ofsource
.
Type Parameters
T
The 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
source
IEnumerable<T>An IEnumerable<T> to extend.
Returns
- IEnumerable<T>
A sequence of
T
with the shuffledsource
.
Type Parameters
T
Remarks
Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Shuffle<T>(IEnumerable<T>, Func<Int32, Int32, Int32>)
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
source
IEnumerable<T>An IEnumerable<T> to extend.
randomizer
Func<Int32, Int32, Int32>The function delegate that will handle the randomization of
source
.
Returns
- IEnumerable<T>
A sequence of
T
with 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
source
IEnumerable<KeyValuePair<TKey, TValue>>An IEnumerable<T> to extend.
Returns
- IDictionary<TKey, TValue>
A Dictionary<TKey,TValue> that is equivalent to the specified
source
sequence.
Type Parameters
TKey
The type of keys in the
source
.TValue
The type of values in the
source
.
Exceptions
- ArgumentNullException
source
is null.- ArgumentException
source
contains 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
source
IEnumerable<KeyValuePair<TKey, TValue>>An IEnumerable<T> to extend.
comparer
IEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys.
Returns
- IDictionary<TKey, TValue>
A Dictionary<TKey,TValue> that is equivalent to the specified
source
sequence.
Type Parameters
TKey
The type of keys in the
source
.TValue
The type of values in the
source
.
Exceptions
- ArgumentNullException
source
is null - or -comparer
is null.- ArgumentException
source
contains at least one KeyValuePair<TKey,TValue> that produces duplicate keys for two elements.
ToPagination<T>(IEnumerable<T>, Func<Int32>, 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
source
IEnumerable<T>An IEnumerable<T> to extend.
totalElementCounter
Func<Int32>The total element counter.
setup
Action<PaginationOptions>The PaginationOptions which may be configured.
Returns
- PaginationEnumerable<T>
An instance of PaginationEnumerable<T>.
Type Parameters
T
The type of elements in the collection.
ToPaginationList<T>(IEnumerable<T>, Func<Int32>, 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
source
IEnumerable<T>An IEnumerable<T> to extend.
totalElementCounter
Func<Int32>The total element counter.
setup
Action<PaginationOptions>The PaginationOptions which may be configured.
Returns
- PaginationList<T>
An instance of PaginationList<T>.
Type Parameters
T
The type of elements in the collection.
ToPartitioner<T>(IEnumerable<T>, Int32)
Extends the specified source
to support iterating in partitions.
public static PartitionerEnumerable<T> ToPartitioner<T>(this IEnumerable<T> source, int partitionSize = 128)
Parameters
source
IEnumerable<T>An IEnumerable<T> to extend.
partitionSize
Int32The size of the partitions.
Returns
- PartitionerEnumerable<T>
An instance of PartitionerEnumerable<T>.
Type Parameters
T
The 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
value
TThe
T
to extend.
Returns
- IEnumerable<T>
An IEnumerable<T> sequence with the specified
value
as the only element.
Type Parameters
T
The type of the element of IEnumerable<T>.