Table of Contents

Class EnumerableExtensions

Namespace
HeroKit.Enumerables
Assembly
HeroKit.dll

Provides helpers for sequences that LINQ does not include.

public static class EnumerableExtensions
Inheritance
EnumerableExtensions
Inherited Members

Remarks

Methods that .NET already provides (Chunk, DistinctBy, MinBy, MaxBy, ...) are intentionally not duplicated.

Methods

IsNotNullOrEmpty<T>(IEnumerable<T>?)

Checks whether a sequence has at least one element.

public static bool IsNotNullOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The sequence to check.

Returns

bool

True if the sequence is not null and has an element; otherwise false.

Type Parameters

T

The element type.

IsNullOrEmpty<T>(IEnumerable<T>?)

Checks whether a sequence is null or has no elements.

public static bool IsNullOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The sequence to check.

Returns

bool

True if the sequence is null or empty; otherwise false.

Type Parameters

T

The element type.

Examples

new List<int>().IsNullOrEmpty(); // true

Remarks

Collections are checked by count. Other sequences are enumerated until the first element only.

Page<T>(IEnumerable<T>, int, int)

Returns one page of a sequence.

public static IEnumerable<T> Page<T>(this IEnumerable<T> source, int pageNumber, int pageSize)

Parameters

source IEnumerable<T>

The sequence to page.

pageNumber int

The 1-based page number.

pageSize int

The number of elements per page.

Returns

IEnumerable<T>

The elements of the requested page. A page past the end is empty.

Type Parameters

T

The element type.

Examples

Enumerable.Range(1, 10).Page(2, 3); // 4, 5, 6

Exceptions

ArgumentOutOfRangeException

pageNumber or pageSize is less than 1.

Page<T>(IQueryable<T>, int, int)

Returns one page of a query. Skip and Take are translated by the query provider, for example Entity Framework.

public static IQueryable<T> Page<T>(this IQueryable<T> source, int pageNumber, int pageSize)

Parameters

source IQueryable<T>

The query to page. Order it first for stable pages.

pageNumber int

The 1-based page number.

pageSize int

The number of elements per page.

Returns

IQueryable<T>

The query for the requested page.

Type Parameters

T

The element type.

Exceptions

ArgumentOutOfRangeException

pageNumber or pageSize is less than 1.

ToReadOnlyList<T>(IEnumerable<T>)

Copies a sequence into a read-only list. Later changes to the source do not affect the result.

public static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The sequence to copy.

Returns

IReadOnlyList<T>

A read-only list that cannot be cast back to a mutable collection.

Type Parameters

T

The element type.

WhereIf<T>(IEnumerable<T>, bool, Func<T, bool>)

Filters a sequence only when a condition is true.

public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, bool> predicate)

Parameters

source IEnumerable<T>

The sequence to filter.

condition bool

When false, the sequence is returned unchanged.

predicate Func<T, bool>

The filter to apply when condition is true.

Returns

IEnumerable<T>

The filtered or the original sequence.

Type Parameters

T

The element type.

Examples

products.WhereIf(onlyInStock, p => p.Stock > 0);

WhereIf<T>(IQueryable<T>, bool, Expression<Func<T, bool>>)

Filters a query only when a condition is true. The filter is translated by the query provider, for example Entity Framework.

public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, bool condition, Expression<Func<T, bool>> predicate)

Parameters

source IQueryable<T>

The query to filter.

condition bool

When false, the query is returned unchanged.

predicate Expression<Func<T, bool>>

The filter to apply when condition is true.

Returns

IQueryable<T>

The filtered or the original query.

Type Parameters

T

The element type.