EnumerableExtensions Class
Provides extension methods for IEnumerable<T> for common operations.
public static class EnumerableExtensionsInheritance System.Object → EnumerableExtensions
Methods
EnumerableExtensions.EnqueueAll<T>(this Queue<T>, IEnumerable<T>) Method
Enqueues all elements from a specified collection into the given queue.
public static void EnqueueAll<T>(this System.Collections.Generic.Queue<T> queue, System.Collections.Generic.IEnumerable<T>? input);Type parameters
T
The type of elements contained in the queue and the enumerable collection.
Parameters
queue System.Collections.Generic.Queue<T>
The queue into which elements will be enqueued.
input System.Collections.Generic.IEnumerable<T>
The collection of elements to enqueue. If the collection is null, no action is taken.
EnumerableExtensions.FirstOrDefaultAsync<TSource,TResult>(this IEnumerable<TSource>, Func<TSource,Task<TResult>>) Method
Projects each element of source through an async
selector and returns the first non-null projection, or null
when every projection is null. Short-circuits at the first non-null result -- subsequent
elements are not awaited.
public static System.Threading.Tasks.Task<TResult?> FirstOrDefaultAsync<TSource,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, System.Func<TSource,System.Threading.Tasks.Task<TResult?>> selector)
where TResult : class;Type parameters
TSource
The element type of the sequence.
TResult
The projection result type; constrained to reference so
"no result" can be expressed as null.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence of items to iterate.
selector System.Func<TSource,System.Threading.Tasks.Task<TResult>>
Async projection invoked per element until one returns non-null.
Returns
System.Threading.Tasks.Task<TResult>
The first non-null projection, or null when none produced a value.
Remarks
Designed for short-circuiting pipelines where each step returns either a payload (e.g.
an error object) or null meaning "passed; keep going".
EnumerableExtensions.FlattenTree<T>(this IEnumerable<T>, Func<T,IEnumerable<T>>) Method
Flattens a tree structure into a flat sequence using breadth-first traversal.
public static System.Collections.Generic.IEnumerable<T> FlattenTree<T>(this System.Collections.Generic.IEnumerable<T>? input, System.Func<T,System.Collections.Generic.IEnumerable<T>> childrenSelector);Type parameters
T
The type of elements in the tree.
Parameters
input System.Collections.Generic.IEnumerable<T>
The sequence of root elements.
childrenSelector System.Func<T,System.Collections.Generic.IEnumerable<T>>
A function to retrieve the children of an element.
Returns
System.Collections.Generic.IEnumerable<T>
A flattened sequence of all elements in the tree.
EnumerableExtensions.FlattenTree<T>(this T, Func<T,IEnumerable<T>>) Method
Flattens a tree structure starting from a single root element into a flat sequence using breadth-first traversal.
public static System.Collections.Generic.IEnumerable<T> FlattenTree<T>(this T root, System.Func<T,System.Collections.Generic.IEnumerable<T>> childrenSelector);Type parameters
T
The type of elements in the tree.
Parameters
root T
The root element of the tree.
childrenSelector System.Func<T,System.Collections.Generic.IEnumerable<T>>
A function to retrieve the children of an element.
Returns
System.Collections.Generic.IEnumerable<T>
A flattened sequence of all elements in the tree.
EnumerableExtensions.Materialize<T>(this IEnumerable<T>) Method
Forces a sequence to be evaluated once and returns the result as a non-lazy System.Collections.Generic.IReadOnlyCollection<> that callers can iterate any number of times. Use at any site that needs to enumerate the same System.Collections.Generic.IEnumerable<> more than once (cost estimate + actual call, log + record, etc.) - a lazy LINQ pipeline or an iterator method passed by an upstream caller would otherwise re-execute its source per enumeration.
public static System.Collections.Generic.IReadOnlyCollection<T> Materialize<T>(this System.Collections.Generic.IEnumerable<T> source);Type parameters
T
The type of elements in the sequence.
Parameters
source System.Collections.Generic.IEnumerable<T>
The sequence to materialize.
Returns
System.Collections.Generic.IReadOnlyCollection<T>
A non-lazy collection holding all elements of source.
Remarks
Skips the allocation when source is already a concrete collection
(T[], List<T>, HashSet<T>, etc.) - only lazy /
iterator-method sources pay for a one-shot copy.
EnumerableExtensions.OrEmpty<T>(this IEnumerable<T>) Method
Returns the original sequence, or an empty sequence if the original is null.
public static System.Collections.Generic.IEnumerable<T> OrEmpty<T>(this System.Collections.Generic.IEnumerable<T>? value);Type parameters
T
The type of elements in the sequence.
Parameters
value System.Collections.Generic.IEnumerable<T>
The sequence to return or an empty sequence if this is null.
Returns
System.Collections.Generic.IEnumerable<T>
The original sequence or an empty sequence.
EnumerableExtensions.TravelUp<T>(this T, Func<T,T>) Method
Traverses a hierarchy upwards, starting from a specific item and moving to its parent, grandparent, etc., as determined by a parent selector function.
public static System.Collections.Generic.IEnumerable<T> TravelUp<T>(this T item, System.Func<T,T?> parentSelector)
where T : class;Type parameters
T
The type of elements in the hierarchy.
Parameters
item T
The starting item in the hierarchy.
parentSelector System.Func<T,T>
A function that returns the parent of a given item.
Returns
System.Collections.Generic.IEnumerable<T>
An IEnumerable<T> representing the path from the item upwards in the hierarchy.