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
enumerableIEnumerable<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
firstIEnumerable<T>The first of the two enumerations to compare.
secondIEnumerable<T>The first of the two enumerations to compare.
comparerIEqualityComparer<T>Controls how to compare elements; leave
nullfor default comparer.
Returns
- bool
 trueiffirstcontains any element fromsecond.falseiffirstorsecondis 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
enumerationIEnumerable<T>The list to check.
elementTThe 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
enumerationIEnumerable<T>The sequence of elements to filter.
keySelectorFunc<T, TKey>A function mapping elements to their respective equality keys.
Returns
- IEnumerable<T>
 
Type Parameters
TTKey
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
enumerationIEnumerable<T>predicateFunc<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
enumerationIEnumerable<T>elementT
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
enumerationIEnumerable<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
enumerationIEnumerable<T>The enumeration to generate the hash for.
comparerIEqualityComparer<T>Controls how to compare elements; leave
nullfor 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
enumerationIEnumerable<T>The enumeration to generate the hash for.
comparerIEqualityComparer<T>Controls how to compare elements; leave
nullfor 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
enumerationIEnumerable<T>The elements to check.
expressionFunc<T, TValue>The expression to maximize.
comparerIComparer<TValue>Controls how to compare elements; leave
nullfor default comparer.
Returns
- T
 The element that maximizes the expression.
Type Parameters
TThe type of the elements.
TValueThe type of the
expression.
Exceptions
- InvalidOperationException
 enumerationcontains 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
enumerationIEnumerable<T>The elements to check.
expressionFunc<T, TValue>The expression to minimize.
comparerIComparer<TValue>Controls how to compare elements; leave
nullfor default comparer.
Returns
- T
 The element that minimizes the expression.
Type Parameters
TThe type of the elements.
TValueThe type of the
expression.
Exceptions
- InvalidOperationException
 enumerationcontains 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
elementsIEnumerable<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
firstIEnumerable<T>The first of the two enumerations to compare.
secondIEnumerable<T>The first of the two enumerations to compare.
comparerIEqualityComparer<T>Controls how to compare elements; leave
nullfor 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
nodesIEnumerable<T>The set of nodes to sort.
getDependenciesFunc<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
sourceIEnumerable<TSource>The elements to map.
selectorFunc<TSource, TResult>The selector to execute for each
sourceelement.exceptionHandlerAction<TException>A Callback to be invoked when a
TExceptionis caught.
Returns
- IEnumerable<TResult>
 
Type Parameters
TSourceThe type of the input elements.
TResultThe type of the output elements.
TExceptionThe 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
firstIEnumerable<T>The first of the two enumerations to compare.
secondIEnumerable<T>The first of the two enumerations to compare.
comparerIEqualityComparer<T>Controls how to compare elements; leave
nullfor 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
enumerationIEnumerable<T>
Returns
- IEnumerable<T>
 
Type Parameters
T