Class EnumerableExtensions
Provides extension methods for IEnumerable<T>s.
Namespace: NanoByte.Common.Collections
Assembly: NanoByte.Common.dll
Syntax
public static class EnumerableExtensions : Object
Methods
CloneElements<T>(IEnumerable<T>)
Calls Clone() for every element in a enumeration and returns the results as a new enumeration.
Declaration
public static IEnumerable<T> CloneElements<T>(this IEnumerable<T> enumerable)
where T : ICloneable<T>
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |
ContainsAny<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines whether one enumeration of elements contains any of the elements in another.
Declaration
public static bool ContainsAny<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The first of the two enumerations to compare. |
IEnumerable<T> | second | The first of the two enumerations to compare. |
System.Collections.Generic.IEqualityComparer<T> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
Boolean |
|
Type Parameters
Name | Description |
---|---|
T |
ContainsOrEmpty<T>(IEnumerable<T>, T)
Determines whether the enumeration contains an element or is null.
Declaration
public static bool ContainsOrEmpty<T>(this IEnumerable<T> enumeration, T element)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The list to check. |
T | element | The element to look for. |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Useful for lists that contain an OR-ed list of restrictions, where an empty list means no restrictions.
DistinctBy<T, TKey>(IEnumerable<T>, Func<T, TKey>)
Filters a sequence of elements to remove any duplicates based on the equality of a key extracted from the elements.
Declaration
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumeration, Func<T, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The sequence of elements to filter. |
Func<T, TKey> | keySelector | A function mapping elements to their respective equality keys. |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T | |
TKey |
Except<T>(IEnumerable<T>, T)
Filters a sequence of elements to remove any that are equal to element
.
Declaration
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, T element)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | |
T | element |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |
Except<T>(IEnumerable<T>, Func<T, Boolean>)
Filters a sequence of elements to remove any that match the predicate
.
The opposite of System.Linq.Enumerable.Where``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,System.Boolean}).
Declaration
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, Func<T, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | |
Func<T, Boolean> | predicate |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |
Flatten<T>(IEnumerable<IEnumerable<T>>)
Flattens a list of lists.
Declaration
public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> enumeration)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<IEnumerable<T>> | enumeration |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |
GetSequencedHashCode<T>(IEnumerable<T>, IEqualityComparer<T>)
Generates a hash code for the contents of the enumeration. Changing the elements' order will change the hash.
Declaration
public static int GetSequencedHashCode<T>(this IEnumerable<T> enumeration, IEqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The enumeration to generate the hash for. |
System.Collections.Generic.IEqualityComparer<T> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
Int32 |
Type Parameters
Name | Description |
---|---|
T |
See Also
GetUnsequencedHashCode<T>(IEnumerable<T>, IEqualityComparer<T>)
Generates a hash code for the contents of the enumeration. Changing the elements' order will not change the hash.
Declaration
public static int GetUnsequencedHashCode<T>(this IEnumerable<T> enumeration, IEqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The enumeration to generate the hash for. |
System.Collections.Generic.IEqualityComparer<T> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
Int32 |
Type Parameters
Name | Description |
---|---|
T |
See Also
MaxBy<T, TValue>(IEnumerable<T>, Func<T, TValue>, IComparer<TValue>)
Determines the element in a list that maximizes a specified expression.
Declaration
public static T MaxBy<T, TValue>(this IEnumerable<T> enumeration, Func<T, TValue> expression, IComparer<TValue> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The elements to check. |
Func<T, TValue> | expression | The expression to maximize. |
IComparer<TValue> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
T | The element that maximizes the expression. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements. |
TValue | The type of the |
Exceptions
Type | Condition |
---|---|
InvalidOperationException |
|
MinBy<T, TValue>(IEnumerable<T>, Func<T, TValue>, IComparer<TValue>)
Determines the element in a list that minimizes a specified expression.
Declaration
public static T MinBy<T, TValue>(this IEnumerable<T> enumeration, Func<T, TValue> expression, IComparer<TValue> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration | The elements to check. |
Func<T, TValue> | expression | The expression to minimize. |
IComparer<TValue> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
T | The element that minimizes the expression. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements. |
TValue | The type of the |
Exceptions
Type | Condition |
---|---|
InvalidOperationException |
|
Permutate<T>(IEnumerable<T>)
Generates all possible permutations of a set of elements
.
Declaration
public static IEnumerable<T[]> Permutate<T>(this IEnumerable<T> elements)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | elements |
Returns
Type | Description |
---|---|
IEnumerable<T[]> |
Type Parameters
Name | Description |
---|---|
T |
SequencedEquals<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines whether two enumerations contain the same elements in the same order.
Declaration
public static bool SequencedEquals<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The first of the two enumerations to compare. |
IEnumerable<T> | second | The first of the two enumerations to compare. |
System.Collections.Generic.IEqualityComparer<T> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T |
TopologicalSort<T>(IEnumerable<T>, Func<T, IEnumerable<T>>)
Performs a topological sort of an object graph.
Declaration
public static IEnumerable<T> TopologicalSort<T>(this IEnumerable<T> nodes, Func<T, IEnumerable<T>> getDependencies)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | nodes | The set of nodes to sort. |
Func<T, IEnumerable<T>> | getDependencies | A function that retrieves all dependencies of a node. |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |
Exceptions
Type | Condition |
---|---|
System.IO.InvalidDataException | Cyclic dependency found. |
TrySelect<TSource, TResult, TException>(IEnumerable<TSource>, Func<TSource, TResult>, Action<TException>)
Maps elements using a selector. Calls a handler for specific exceptions, skips the element and continues enumerating with the element.
Declaration
public static IEnumerable<TResult> TrySelect<TSource, TResult, TException>(this IEnumerable<TSource> source, Func<TSource, TResult> selector, Action<TException> exceptionHandler)
where TException : Exception
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The elements to map. |
Func<TSource, TResult> | selector | The selector to execute for each |
Action<TException> | exceptionHandler | A Callback to be invoked when a |
Returns
Type | Description |
---|---|
IEnumerable<TResult> |
Type Parameters
Name | Description |
---|---|
TSource | The type of the input elements. |
TResult | The type of the output elements. |
TException | The type of exceptions to handle.. |
UnsequencedEquals<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Determines whether two enumerations contain the same elements disregarding the order they are in.
Declaration
public static bool UnsequencedEquals<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The first of the two enumerations to compare. |
IEnumerable<T> | second | The first of the two enumerations to compare. |
System.Collections.Generic.IEqualityComparer<T> | comparer | Controls how to compare elements; leave |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T |
WhereNotNull<T>(IEnumerable<T>)
Filters a sequence of elements to remove any null
values.
Declaration
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T> enumeration)
where T : class
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumeration |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T |