Class EnumerableExtensions
- Namespace
- NanoByte.Common.Collections
- Assembly
- NanoByte.Common.dll
Provides extension methods for System.Collections.Generic.IEnumerable<T>s.
public static class EnumerableExtensions
- Inheritance
-
objectEnumerableExtensions
Methods
CloneElements<T>(IEnumerable<T>)
Calls Clone() for every element in a enumeration and returns the results as a new enumeration.
[LinqTunnel]
public static IEnumerable<T> CloneElements<T>(this IEnumerable<T> enumerable) where T : ICloneable<T>
Parameters
enumerable
IEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
ContainsAny<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>?)
Determines whether one enumeration of elements contains any of the elements in another.
[Pure]
public static bool ContainsAny<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer = null)
Parameters
first
IEnumerable<T>The first of the two enumerations to compare.
second
IEnumerable<T>The first of the two enumerations to compare.
comparer
IEqualityComparer<T>Controls how to compare elements; leave
null
for default comparer.
Returns
- bool
true
iffirst
contains any element fromsecond
.false
iffirst
orsecond
is empty.
Type Parameters
T
ContainsOrEmpty<T>(IEnumerable<T>, T)
Determines whether the enumeration contains an element or is null.
[Pure]
public static bool ContainsOrEmpty<T>(this IEnumerable<T> enumeration, T element)
Parameters
enumeration
IEnumerable<T>The list to check.
element
TThe element to look for.
Returns
- bool
Type Parameters
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.
[LinqTunnel]
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumeration, Func<T, TKey> keySelector) where T : notnull where TKey : notnull
Parameters
enumeration
IEnumerable<T>The sequence of elements to filter.
keySelector
Func<T, TKey>A function mapping elements to their respective equality keys.
Returns
- IEnumerable<T>
Type Parameters
T
TKey
Except<T>(IEnumerable<T>, Func<T, bool>)
Filters a sequence of elements to remove any that match the predicate
.
The opposite of System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource, bool>).
[LinqTunnel]
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, Func<T, bool> predicate)
Parameters
enumeration
IEnumerable<T>predicate
Func<T, bool>
Returns
- IEnumerable<T>
Type Parameters
T
Except<T>(IEnumerable<T>, T)
Filters a sequence of elements to remove any that are equal to element
.
[LinqTunnel]
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, T element)
Parameters
enumeration
IEnumerable<T>element
T
Returns
- IEnumerable<T>
Type Parameters
T
Flatten<T>(IEnumerable<IEnumerable<T>>)
Flattens a list of lists.
[LinqTunnel]
public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> enumeration)
Parameters
enumeration
IEnumerable<IEnumerable<T>>
Returns
- IEnumerable<T>
Type Parameters
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.
[Pure]
public static int GetSequencedHashCode<T>(this IEnumerable<T> enumeration, IEqualityComparer<T>? comparer = null)
Parameters
enumeration
IEnumerable<T>The enumeration to generate the hash for.
comparer
IEqualityComparer<T>Controls how to compare elements; leave
null
for default comparer.
Returns
- int
Type Parameters
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.
[Pure]
public static int GetUnsequencedHashCode<T>(this IEnumerable<T> enumeration, IEqualityComparer<T>? comparer = null)
Parameters
enumeration
IEnumerable<T>The enumeration to generate the hash for.
comparer
IEqualityComparer<T>Controls how to compare elements; leave
null
for default comparer.
Returns
- int
Type Parameters
T
- See Also
MaxBy<T, TValue>(IEnumerable<T>, Func<T, TValue>, IComparer<TValue>?)
Determines the element in a list that maximizes a specified expression.
[Pure]
public static T MaxBy<T, TValue>(this IEnumerable<T> enumeration, Func<T, TValue> expression, IComparer<TValue>? comparer = null)
Parameters
enumeration
IEnumerable<T>The elements to check.
expression
Func<T, TValue>The expression to maximize.
comparer
IComparer<TValue>Controls how to compare elements; leave
null
for default comparer.
Returns
- T
The element that maximizes the expression.
Type Parameters
T
The type of the elements.
TValue
The type of the
expression
.
Exceptions
- InvalidOperationException
enumeration
contains no elements.
MinBy<T, TValue>(IEnumerable<T>, Func<T, TValue>, IComparer<TValue>?)
Determines the element in a list that minimizes a specified expression.
[Pure]
public static T MinBy<T, TValue>(this IEnumerable<T> enumeration, Func<T, TValue> expression, IComparer<TValue>? comparer = null)
Parameters
enumeration
IEnumerable<T>The elements to check.
expression
Func<T, TValue>The expression to minimize.
comparer
IComparer<TValue>Controls how to compare elements; leave
null
for default comparer.
Returns
- T
The element that minimizes the expression.
Type Parameters
T
The type of the elements.
TValue
The type of the
expression
.
Exceptions
- InvalidOperationException
enumeration
contains no elements.
Permutate<T>(IEnumerable<T>)
Generates all possible permutations of a set of elements
.
[LinqTunnel]
public static IEnumerable<T[]> Permutate<T>(this IEnumerable<T> elements)
Parameters
elements
IEnumerable<T>
Returns
- IEnumerable<T[]>
Type Parameters
T
SequencedEquals<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>?)
Determines whether two enumerations contain the same elements in the same order.
[Pure]
public static bool SequencedEquals<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer = null)
Parameters
first
IEnumerable<T>The first of the two enumerations to compare.
second
IEnumerable<T>The first of the two enumerations to compare.
comparer
IEqualityComparer<T>Controls how to compare elements; leave
null
for default comparer.
Returns
- bool
Type Parameters
T
TopologicalSort<T>(IEnumerable<T>, Func<T, IEnumerable<T>>)
Performs a topological sort of an object graph.
[Pure]
public static IEnumerable<T> TopologicalSort<T>(this IEnumerable<T> nodes, Func<T, IEnumerable<T>> getDependencies)
Parameters
nodes
IEnumerable<T>The set of nodes to sort.
getDependencies
Func<T, IEnumerable<T>>A function that retrieves all dependencies of a node.
Returns
- IEnumerable<T>
Type Parameters
T
Exceptions
- 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.
[LinqTunnel]
public static IEnumerable<TResult> TrySelect<TSource, TResult, TException>(this IEnumerable<TSource> source, Func<TSource, TResult> selector, Action<TException> exceptionHandler) where TException : Exception
Parameters
source
IEnumerable<TSource>The elements to map.
selector
Func<TSource, TResult>The selector to execute for each
source
element.exceptionHandler
Action<TException>A Callback to be invoked when a
TException
is caught.
Returns
- IEnumerable<TResult>
Type Parameters
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.
[Pure]
public static bool UnsequencedEquals<T>(this IEnumerable<T> first, IEnumerable<T> second, IEqualityComparer<T>? comparer = null)
Parameters
first
IEnumerable<T>The first of the two enumerations to compare.
second
IEnumerable<T>The first of the two enumerations to compare.
comparer
IEqualityComparer<T>Controls how to compare elements; leave
null
for default comparer.
Returns
- bool
Type Parameters
T
WhereNotNull<T>(IEnumerable<T?>)
Filters a sequence of elements to remove any null
values.
[LinqTunnel]
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumeration) where T : class
Parameters
enumeration
IEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T