IEnumerableExtensions
LINQ-style extension methods for System.Collections.Generic.IEnumerable<T>. Adds iteration utilities (ForEach, With), exclusion helpers (Except, ExceptNull), conditional append/prepend, conditional ordering, and a tuple-aware Select overload.
Definition
Namespace: Paragon Assembly: Paragon.dll
public static class IEnumerableExtensionsQuick Lookup
Execute action on each element
source.ForEach(action)
Execute action and continue chaining
source.With(action)
Exclude a single item
source.Except(item)
Exclude multiple items
source.Except(item1, item2, ...)
Remove nulls
source.ExceptNull()
Append only if condition is met
source.AppendIf(condition, item)
Prepend only if condition is met
source.PrependIf(condition, item)
Order only if condition is met
source.OrderByIf(condition, keySelector)
Order descending conditionally
source.OrderByDescendingIf(condition, keySelector)
Select from tuple sequence
source.Select((a, b) => ...)
Methods
ForEach
Performs the specified action on each element. Terminal operation — consumes the sequence immediately.
source
IEnumerable<TSource>
The sequence to iterate
action
Action<TSource>
Action to invoke on each element
With
Invokes the specified action on each element while yielding them through the pipeline. Unlike ForEach, this is a deferred/lazy operation that can be chained with other LINQ operators.
source
IEnumerable<TSource>
The sequence to iterate
action
Action<TSource>
Side-effect action on each element
Returns: Lazy IEnumerable<TSource> yielding each element after applying the action.
Except (Single Item)
Returns elements that do not match the specified item, using EqualityComparer<TSource>.Default.
source
IEnumerable<TSource>
The sequence to filter
item
TSource
Item to exclude
Returns: Lazy IEnumerable<TSource> excluding all occurrences of item.
Unlike LINQ's built-in Enumerable.Except(IEnumerable) which removes distinct elements from a second sequence, this overload removes all occurrences of a single item. It also does not deduplicate the source sequence.
Except (Multiple Items)
Returns elements that do not appear in the specified params array.
source
IEnumerable<TSource>
The sequence to filter
items
TSource[]
Items to exclude
Returns: IEnumerable<TSource> excluding elements found in items. Delegates to LINQ's Enumerable.Except(IEnumerable, IEqualityComparer).
ExceptNull
Returns only non-null elements from the sequence.
Returns: Lazy IEnumerable<TSource> excluding null values.
AppendIf
Appends an element to the end of the sequence only if the condition is true.
source
IEnumerable<TSource>
The sequence
condition
bool
Whether to append
append
TSource
Element to append
Returns: Lazy IEnumerable<TSource> with the element appended if condition is true.
PrependIf
Prepends an element to the beginning of the sequence only if the condition is true.
source
IEnumerable<TSource>
The sequence
condition
bool
Whether to prepend
prepend
TSource
Element to prepend
Returns: Lazy IEnumerable<TSource> with the element prepended if condition is true.
OrderByIf
Conditionally orders the sequence in ascending order by the specified key. If condition is true, the sequence is ordered; otherwise, it is returned unchanged.
source
IEnumerable<TSource>
The sequence
condition
bool
Whether to apply ordering
orderBy
Func<TSource, TKey>
Key selector for ordering
Returns: Ordered or unchanged IEnumerable<TSource>.
OrderByDescendingIf
Conditionally orders the sequence in descending order by the specified key.
source
IEnumerable<TSource>
The sequence
condition
bool
Whether to apply ordering
orderBy
Func<TSource, TKey>
Key selector for ordering
Returns: Ordered or unchanged IEnumerable<TSource>.
Select (Tuple Overload)
Projects each element of a sequence of ValueTuple<T1, T2> into a new form using a two-parameter selector, avoiding manual tuple deconstruction.
source
IEnumerable<(T1, T2)>
Sequence of tuples
selector
Func<T1, T2, TResult>
Transform function receiving deconstructed tuple elements
Returns: Lazy IEnumerable<TResult> of projected values.
Common Pitfalls
ForEach is terminal, With is deferred. ForEach consumes the sequence immediately (like a foreach loop). With is lazy and only executes when the returned IEnumerable is enumerated. If you call source.With(action) and never enumerate the result, the action is never executed.
Except(single) does not deduplicate. The single-item Except(TSource item) overload removes all occurrences of item but does not deduplicate the rest of the sequence. The params overload Except(params TSource[]) delegates to LINQ's Enumerable.Except which does deduplicate.
Examples
Chaining Extensions
Tuple Select
See Also
ICollectionExtensions — collection-level utilities
Extensions — extension system overview
Last updated