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 IEnumerableExtensions

Quick Lookup

Goal
Method

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

source

IEnumerable<TSource>

The sequence to filter

item

TSource

Item to exclude

Returns: Lazy IEnumerable<TSource> excluding all occurrences of item.

circle-info

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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

circle-exclamation
circle-exclamation

Examples

Chaining Extensions

Tuple Select

See Also

Last updated