Show / Hide Table of Contents

Class EnumerableExtensions

Extensions for all items of type IEnumerable<T>.

Inheritance
object
EnumerableExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class EnumerableExtensions

Methods

| Edit this page View Source

AreAllEqual<T>(IEnumerable<T>)

Checks whether all objects in the collection are equal.

Declaration
public static bool AreAllEqual<T>(this IEnumerable<T> c)
Parameters
Type Name Description
IEnumerable<T> c

Collection containing the objects to check.

Returns
Type Description
bool

true if all objects in the collection are equal, false otherwise.

Type Parameters
Name Description
T

Type of objects in the collection.

| Edit this page View Source

AreAllEqual<T, TProp>(IEnumerable<T>, Func<T, TProp>)

Checks whether the property or value within all objects in the collection are equal.

Declaration
public static bool AreAllEqual<T, TProp>(this IEnumerable<T> c, Func<T, TProp> selector)
Parameters
Type Name Description
IEnumerable<T> c

Collection containing the objects to check.

Func<T, TProp> selector

Selector function for the value of each object.

Returns
Type Description
bool

true if all selected values from the objects in the collection are equal, false otherwise.

Type Parameters
Name Description
T

Type of objects in the collection.

TProp

Type of value to compare.

| Edit this page View Source

Contains(IEnumerable, object?)

Non-generic version of the function Contains<TSource>(IEnumerable<TSource>, TSource).

Declaration
public static bool Contains(this IEnumerable enumerable, object? obj)
Parameters
Type Name Description
IEnumerable enumerable

Collection to check.

object obj

Object to search within the collection.

Returns
Type Description
bool

true if the collection contains the specified object, false otherwise.

| Edit this page View Source

ContainsAll(IEnumerable, IEnumerable)

Checks if the collection contains all the elements of the items collection.

Declaration
public static bool ContainsAll(this IEnumerable collection, IEnumerable items)
Parameters
Type Name Description
IEnumerable collection

Enumeration to check.

IEnumerable items

Elements that must exist in collection.

Returns
Type Description
bool

true if collection contains all the elements of items, false otherwise.

| Edit this page View Source

ContainsAll(IEnumerable, params object?[])

Checks if the collection contains all the elements of the items collection.

Declaration
public static bool ContainsAll(this IEnumerable collection, params object?[] items)
Parameters
Type Name Description
IEnumerable collection

Enumeration to check.

object[] items

Elements that must exist in collection.

Returns
Type Description
bool

true if collection contains all the elements of items, false otherwise.

| Edit this page View Source

ContainsAny(IEnumerable, IEnumerable)

Checks if the collection contains any of the elements of the items collection.

Declaration
public static bool ContainsAny(this IEnumerable collection, IEnumerable items)
Parameters
Type Name Description
IEnumerable collection

Enumeration to check.

IEnumerable items

Elements that must exist in collection.

Returns
Type Description
bool

true if collection contains any of the elements of items, false otherwise.

| Edit this page View Source

ContainsAny(IEnumerable, params object?[])

Checks if the collection contains any of the elements of the items collection.

Declaration
public static bool ContainsAny(this IEnumerable collection, params object?[] items)
Parameters
Type Name Description
IEnumerable collection

Enumeration to check.

object[] items

Elements that must exist in collection.

Returns
Type Description
bool

true if collection contains any of the elements of items, false otherwise.

| Edit this page View Source

Copy<T>(IEnumerable<T>)

Returns a copy of the elements of this IEnumerable<T>

Declaration
public static IEnumerable<T> Copy<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection

Collection to copy.

Returns
Type Description
IEnumerable<T>

Copy of this list. The elements of the copy represent the same instance of the original object.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Count(IEnumerable)

Non-generic version of the function Count<TSource>(IEnumerable<TSource>). Gets the number of elements in a sequence.

Declaration
public static int Count(this IEnumerable e)
Parameters
Type Name Description
IEnumerable e

Sequence to check. The same will be enumerated.

Returns
Type Description
int

The number of elements within the sequence.

| Edit this page View Source

ExceptFor<T>(IEnumerable<T>, params T[])

Enumerates all elements of the collection, omitting the specified ones.

Declaration
public static IEnumerable<T> ExceptFor<T>(this IEnumerable<T> collection, params T[] exclusions)
Parameters
Type Name Description
IEnumerable<T> collection

Collection to enumerate.

T[] exclusions

Elements to exclude from the collection.

Returns
Type Description
IEnumerable<T>

An enumeration with the elements of the collection, omitting the specified exclusions.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

FindIndexOf<T>(IEnumerable<T>, T)

Finds the index of an object within a collection.

Declaration
public static int FindIndexOf<T>(this IEnumerable<T> e, T item)
Parameters
Type Name Description
IEnumerable<T> e

Collection containing the objects to check.

T item

Item to get the index of.

Returns
Type Description
int

The index of the specified object, or -1 if the object does not exist within the collection.

Type Parameters
Name Description
T

Type of objects in the collection.

| Edit this page View Source

FirstOf<T>(IEnumerable<T>, Type)

Gets the first element of the requested type within a collection.

Declaration
public static T FirstOf<T>(this IEnumerable<T> collection, Type type)
Parameters
Type Name Description
IEnumerable<T> collection

Collection over which to perform the search.

Type type

Type of element to search for.

Returns
Type Description
T

The first element of type type that is found in the collection, or default if no element of the specified type is found.

Type Parameters
Name Description
T

Type of elements contained in the collection.

| Edit this page View Source

FirstOf<T>(IEnumerable)

Gets the first element of the requested type within a collection.

Declaration
public static T FirstOf<T>(this IEnumerable collection)
Parameters
Type Name Description
IEnumerable collection

Collection on which to perform the search.

Returns
Type Description
T

The first element of type T that is found in the collection, or default if no element of the specified type is found.

Type Parameters
Name Description
T

Type of element to search for.

| Edit this page View Source

GroupByType(IEnumerable)

Groups a sequence of elements according to their types.

Declaration
public static IEnumerable<IGrouping<Type, object>> GroupByType(this IEnumerable c)
Parameters
Type Name Description
IEnumerable c

Collection to group.

Returns
Type Description
IEnumerable<IGrouping<Type, object>>

A sequence of elements grouped according to their type.

| Edit this page View Source

IsAnyOf(IEnumerable, Type)

Checks whether the collection contains at least one element of the specified type.

Declaration
public static bool IsAnyOf(this IEnumerable collection, Type type)
Parameters
Type Name Description
IEnumerable collection

Collection of elements to check.

Type type

Type of object to search for.

Returns
Type Description
bool

true if there is an element of the specified type in the collection, false otherwise.

| Edit this page View Source

IsAnyOf<T>(IEnumerable)

Checks whether the collection contains at least one element of the specified type.

Declaration
public static bool IsAnyOf<T>(this IEnumerable collection)
Parameters
Type Name Description
IEnumerable collection

Collection of elements to check.

Returns
Type Description
bool

true if there is an element of the specified type in the collection, false otherwise.

Type Parameters
Name Description
T

Type of object to search for.

| Edit this page View Source

IsPropertyEqual<T>(IEnumerable<T>, Func<T, object>)

Gets a value that indicates whether the value of the property of all objects in the collection is equal.

Declaration
public static bool IsPropertyEqual<T>(this IEnumerable<T> c, Func<T, object> selector)
Parameters
Type Name Description
IEnumerable<T> c

Collection containing the objects to check.

Func<T, object> selector

Selector function.

Returns
Type Description
bool

true if the value of the property of all objects in the collection is the same, false otherwise.

Type Parameters
Name Description
T

Type of objects in the collection.

| Edit this page View Source

IsQuorum<T>(IEnumerable<T>, int, out T)

Determines whether a quorum is met for a specified value in a collection.

Declaration
public static bool IsQuorum<T>(this IEnumerable<T> values, int quorumCount, out T value) where T : notnull
Parameters
Type Name Description
IEnumerable<T> values

The collection of values to evaluate.

int quorumCount

The minimum number of occurrences required for a value to meet the quorum. Must be greater than or equal to 1.

T value

When this method returns true, contains the value that meets the quorum. When this method returns false, contains the default value for the type T.

Returns
Type Description
bool

true if a value in the collection meets or exceeds the specified quorum count; otherwise, false.

Type Parameters
Name Description
T

The type of elements in the collection. Must be a non-nullable type.

Remarks

This method evaluates the collection to determine if any value appears at least quorumCount times. If multiple values meet the quorum, the value with the highest occurrence is selected. If there is a tie, the first value encountered in the collection is returned.

| Edit this page View Source

ItemsEqual(IEnumerable, IEnumerable)

Compares two collections and determines if their elements are equal.

Declaration
public static bool ItemsEqual(this IEnumerable collection, IEnumerable items)
Parameters
Type Name Description
IEnumerable collection

Enumeration to check.

IEnumerable items

Enumeration against which to check.

Returns
Type Description
bool

true if the elements of both collections are equal, false otherwise.

| Edit this page View Source

Locked<T>(T, Action<T>)

Executes an operation on a sequence in a locked context.

Declaration
public static void Locked<T>(this T collection, Action<T> action) where T : IEnumerable
Parameters
Type Name Description
T collection

Sequence on which to execute a locked operation.

Action<T> action

Action to execute on the sequence.

Type Parameters
Name Description
T

Type of elements in the sequence.

| Edit this page View Source

Locked<T, TResult>(T, Func<T, TResult>)

Executes an operation on a sequence in a locked context.

Declaration
public static TResult Locked<T, TResult>(this T collection, Func<T, TResult> function) where T : IEnumerable
Parameters
Type Name Description
T collection

Sequence on which to execute a locked operation.

Func<T, TResult> function

Function to execute on the sequence.

Returns
Type Description
TResult
Type Parameters
Name Description
T

Type of elements in the sequence.

TResult

Type of result obtained by the function.

| Edit this page View Source

NonDefaults<T>(IEnumerable<T?>)

Enumerates the elements that are distinct from their default value within a collection.

Declaration
public static IEnumerable<T> NonDefaults<T>(this IEnumerable<T?> collection) where T : notnull
Parameters
Type Name Description
IEnumerable<T> collection

Collection to enumerate.

Returns
Type Description
IEnumerable<T>

An enumeration with the elements of the collection, omitting those that are equal to their default value, or in the case of reference types, omitting those that are null.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

NotNull(IEnumerable?)

Enumerates the non-null elements of a collection.

Declaration
public static IEnumerable NotNull(this IEnumerable? collection)
Parameters
Type Name Description
IEnumerable collection

Collection to enumerate.

Returns
Type Description
IEnumerable

An enumeration with the elements of the collection, omitting those that are null, or a collection empty if collection is null.

| Edit this page View Source

NotNull<T>(IEnumerable<T?>?)

Enumerates the non-null elements of a collection.

Declaration
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?>? collection) where T : struct
Parameters
Type Name Description
IEnumerable<T?> collection

Collection to enumerate.

Returns
Type Description
IEnumerable<T>

An enumeration with the non-null elements of the collection, or an empty collection if collection is null.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

NotNull<T>(IEnumerable<T?>?)

Enumerates the non-null elements of a collection.

Declaration
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?>? collection) where T : class
Parameters
Type Name Description
IEnumerable<T> collection

Collection to enumerate.

Returns
Type Description
IEnumerable<T>

An enumeration with the non-null elements of the collection, or an empty collection if collection is null.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

NullCount(IEnumerable)

Gets the count of null elements within a sequence.

Declaration
public static int NullCount(this IEnumerable collection)
Parameters
Type Name Description
IEnumerable collection

Sequence from which to get the count of null elements.

Returns
Type Description
int

The count of null elements within the collection.

| Edit this page View Source

OfType<T>(IEnumerable<T>, Type)

Enumerates all elements of the collection that are of the specified type.

Declaration
public static IEnumerable<T> OfType<T>(this IEnumerable<T> collection, Type type)
Parameters
Type Name Description
IEnumerable<T> collection

Collection over which to perform the search.

Type type

Type of elements to return.

Returns
Type Description
IEnumerable<T>

An enumeration of all elements of the collection that are of the specified type.

Type Parameters
Name Description
T

Type of elements contained in the collection.

| Edit this page View Source

OrNull<T>(IEnumerable<T>)

Enumerates the collection, and returns null if the collection contains no elements.

Declaration
public static IEnumerable<T>? OrNull<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection

Collection to enumerate.

Returns
Type Description
IEnumerable<T>

An enumeration with the elements of the collection, or null if the collection contains no elements.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

Ordered<T>(IEnumerable<T>)

Iterates in sorted order over the collection.

Declaration
public static IOrderedEnumerable<T> Ordered<T>(this IEnumerable<T> collection) where T : IComparable<T>
Parameters
Type Name Description
IEnumerable<T> collection

Collection to iterate over

Returns
Type Description
IOrderedEnumerable<T>

An ordered enumerable object created from the collection.

Type Parameters
Name Description
T

Type of elements in the collection.

| Edit this page View Source

PickAsync<T>(IEnumerable<T>)

Selects a random element from the collection asynchronously.

Declaration
public static Task<T> PickAsync<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection
Returns
Type Description
Task<T>

A random object from the collection.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Pick<T>(IEnumerable<T>)

Selects a random element from the collection.

Declaration
public static T Pick<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection

Collection from which to select.

Returns
Type Description
T

A random object from the collection.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Pick<T>(IEnumerable<T>, in Random)

Selects a random element from the collection.

Declaration
public static T Pick<T>(this IEnumerable<T> collection, in Random random)
Parameters
Type Name Description
IEnumerable<T> collection

Collection from which to select.

Random random

Random number generator to use.

Returns
Type Description
T

A random object from the collection.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Quorum<T>(IEnumerable<T>, int)

Determines the most frequently occurring value in the provided collection that meets the specified quorum count.

Declaration
public static T Quorum<T>(this IEnumerable<T> values, int quorumCount) where T : notnull
Parameters
Type Name Description
IEnumerable<T> values

The collection of values to evaluate. Cannot be null.

int quorumCount

The minimum number of occurrences required for a value to be considered a quorum.

Returns
Type Description
T

The value that meets the quorum count and occurs most frequently in the collection.

Type Parameters
Name Description
T

The type of the values in the collection. Must be a non-nullable type.

Exceptions
Type Condition
InvalidOperationException

Thrown if no value in the collection meets the specified quorum count.

| Edit this page View Source

Range<T>(IEnumerable<T>, int, int)

Gets a sub-range of values within this IEnumerable<T>.

Declaration
public static IEnumerable<T> Range<T>(this IEnumerable<T> from, int index, int count)
Parameters
Type Name Description
IEnumerable<T> from

IEnumerable<T> from which to extract the sequence.

int index

Index from which to obtain the sub-range.

int count

Quantity of elements to obtain.

Returns
Type Description
IEnumerable<T>

An IEnumerable<T> that contains the specified sub-range.

Type Parameters
Name Description
T
Exceptions
Type Condition
IndexOutOfRangeException

Thrown if index is out of the range of the collection.

| Edit this page View Source

Rotate<T>(IEnumerable<T>, int)

Rotates the elements of an array, list, or collection.

Declaration
public static IEnumerable<T> Rotate<T>(this IEnumerable<T> collection, int steps)
Parameters
Type Name Description
IEnumerable<T> collection

Array to rotate

int steps

Direction and units of rotation.

Returns
Type Description
IEnumerable<T>
Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

Remarks

If steps is positive, the rotation occurs upwards; otherwise, downwards.

| Edit this page View Source

SelectAsync<TIn, TOut>(IEnumerable<TIn>, Func<TIn, Task<TOut>>)

Transforms an input enumeration to the required output type asynchronously.

Declaration
public static IAsyncEnumerable<TOut> SelectAsync<TIn, TOut>(this IEnumerable<TIn> input, Func<TIn, Task<TOut>> selector)
Parameters
Type Name Description
IEnumerable<TIn> input

Input enumeration.

Func<TIn, Task<TOut>> selector

Asynchronous task that will transform the input data.

Returns
Type Description
IAsyncEnumerable<TOut>

An IAsyncEnumerable<T> that can be used to await the enumeration of each element.

Type Parameters
Name Description
TIn

Type of elements in the input enumeration.

TOut

Type of elements in the output enumeration.

| Edit this page View Source

Shift<T>(IEnumerable<T>, int)

Shifts the elements of an array, list, or collection.

Declaration
public static IEnumerable<T> Shift<T>(this IEnumerable<T> collection, int steps)
Parameters
Type Name Description
IEnumerable<T> collection

Array to shift

int steps

Direction and units of shift.

Returns
Type Description
IEnumerable<T>
Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

Remarks

If steps is positive, the shift occurs upwards; otherwise, downwards.

| Edit this page View Source

Shuffled<T>(IEnumerable<T>)

Returns an unsorted version of the IEnumerable<T> without altering the original collection.

Declaration
public static IEnumerable<T> Shuffled<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection

IEnumerable<T> to shuffle.

Returns
Type Description
IEnumerable<T>

An unsorted version of the IEnumerable<T>.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Shuffled<T>(IEnumerable<T>, in int)

Returns an unsorted version of the IEnumerable<T> without altering the original collection.

Declaration
public static IEnumerable<T> Shuffled<T>(this IEnumerable<T> collection, in int deepness)
Parameters
Type Name Description
IEnumerable<T> collection

IEnumerable<T> to shuffle.

int deepness

Depth of the shuffle. 1 is the highest.

Returns
Type Description
IEnumerable<T>

An unsorted version of the IEnumerable<T>.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Shuffled<T>(IEnumerable<T>, in int, in int)

Returns an unsorted version of the specified range of elements of the IEnumerable<T> without altering the original collection.

Declaration
public static IEnumerable<T> Shuffled<T>(this IEnumerable<T> collection, in int firstIdx, in int lastIdx)
Parameters
Type Name Description
IEnumerable<T> collection

IEnumerable<T> to shuffle.

int firstIdx

Starting index of the range.

int lastIdx

Starting index of the range.

Returns
Type Description
IEnumerable<T>

An unsorted version of the specified range of elements of the IEnumerable<T>.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Shuffled<T>(IEnumerable<T>, in int, in int, in int, in Random)

Returns an unsorted version of the specified range of elements of the IEnumerable<T> without altering the original collection.

Declaration
public static IEnumerable<T> Shuffled<T>(this IEnumerable<T> collection, in int firstIdx, in int lastIdx, in int deepness, in Random random)
Parameters
Type Name Description
IEnumerable<T> collection

IEnumerable<T> to shuffle.

int firstIdx

Starting index of the range.

int lastIdx

Starting index of the range.

int deepness

Depth of the shuffle. 1 is the highest.

Random random

Random number generator to use.

Returns
Type Description
IEnumerable<T>

An unsorted version of the specified range of elements of the IEnumerable<T>.

Type Parameters
Name Description
T

Type of elements contained in the IEnumerable<T>.

| Edit this page View Source

Sum(IEnumerable<TimeSpan>)

Calculates the total duration by summing all TimeSpan values in the collection.

Declaration
public static TimeSpan Sum(this IEnumerable<TimeSpan> collection)
Parameters
Type Name Description
IEnumerable<TimeSpan> collection

The collection of TimeSpan values to sum. Cannot be null.

Returns
Type Description
TimeSpan

A TimeSpan representing the total duration of all elements in the collection. Returns Zero if the collection is empty.

| Edit this page View Source

ToExtendedListAsync<T>(IEnumerable<T>)

Creates a ListEx<T> from an IEnumerable<T> asynchronously.

Declaration
[Obsolete("The class is not supported by MCART anymore.")]
public static Task<ListEx<T>> ToExtendedListAsync<T>(this IEnumerable<T> enumerable)
Parameters
Type Name Description
IEnumerable<T> enumerable
Returns
Type Description
Task<ListEx<T>>

A task that can be used to monitor the operation.

Type Parameters
Name Description
T

Type of the collection.

| Edit this page View Source

ToExtendedList<T>(IEnumerable<T>)

Creates a ListEx<T> from an IEnumerable<T>.

Declaration
[Obsolete("The class is not supported by MCART anymore.")]
public static ListEx<T> ToExtendedList<T>(this IEnumerable<T> collection)
Parameters
Type Name Description
IEnumerable<T> collection

Collection to convert

Returns
Type Description
ListEx<T>

A ListEx<T> from the TheXDS.MCART.Types.Extensions namespace.

Type Parameters
Name Description
T

Type of the collection.

| Edit this page View Source

ToGeneric(IEnumerable)

Enumerates a non-generic collection as a generic one.

Declaration
public static IEnumerable<object?> ToGeneric(this IEnumerable collection)
Parameters
Type Name Description
IEnumerable collection

Collection to enumerate.

Returns
Type Description
IEnumerable<object>

An enumeration with the contents of the non-generic enumeration exposed as a generic one.

| Edit this page View Source

ToListAsync<T>(IEnumerable<T>)

Creates a List<T> from an IEnumerable<T> asynchronously.

Declaration
public static Task<List<T>> ToListAsync<T>(this IEnumerable<T> enumerable)
Parameters
Type Name Description
IEnumerable<T> enumerable
Returns
Type Description
Task<List<T>>

A task that can be used to monitor the operation.

Type Parameters
Name Description
T

Type of the collection.

| Edit this page View Source

YieldAsync<T>(IEnumerable<T>, Func<T, Task>)

Executes a task asynchronously over each item to be enumerated.

Declaration
public static IAsyncEnumerable<T> YieldAsync<T>(this IEnumerable<T> input, Func<T, Task> processor)
Parameters
Type Name Description
IEnumerable<T> input

Input enumeration.

Func<T, Task> processor

Asynchronous task that will be executed over each item before being enumerated.

Returns
Type Description
IAsyncEnumerable<T>

An IAsyncEnumerable<T> that can be used to await the enumeration of each element.

Type Parameters
Name Description
T

Type of elements in the enumeration.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX