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
sourceIEnumerable<T>The sequence to check.
Returns
- bool
True if the sequence is not null and has an element; otherwise false.
Type Parameters
TThe 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
sourceIEnumerable<T>The sequence to check.
Returns
- bool
True if the sequence is null or empty; otherwise false.
Type Parameters
TThe 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
sourceIEnumerable<T>The sequence to page.
pageNumberintThe 1-based page number.
pageSizeintThe number of elements per page.
Returns
- IEnumerable<T>
The elements of the requested page. A page past the end is empty.
Type Parameters
TThe element type.
Examples
Enumerable.Range(1, 10).Page(2, 3); // 4, 5, 6
Exceptions
- ArgumentOutOfRangeException
pageNumberorpageSizeis 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
sourceIQueryable<T>The query to page. Order it first for stable pages.
pageNumberintThe 1-based page number.
pageSizeintThe number of elements per page.
Returns
- IQueryable<T>
The query for the requested page.
Type Parameters
TThe element type.
Exceptions
- ArgumentOutOfRangeException
pageNumberorpageSizeis 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
sourceIEnumerable<T>The sequence to copy.
Returns
- IReadOnlyList<T>
A read-only list that cannot be cast back to a mutable collection.
Type Parameters
TThe 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
sourceIEnumerable<T>The sequence to filter.
conditionboolWhen false, the sequence is returned unchanged.
predicateFunc<T, bool>The filter to apply when
conditionis true.
Returns
- IEnumerable<T>
The filtered or the original sequence.
Type Parameters
TThe 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
sourceIQueryable<T>The query to filter.
conditionboolWhen false, the query is returned unchanged.
predicateExpression<Func<T, bool>>The filter to apply when
conditionis true.
Returns
- IQueryable<T>
The filtered or the original query.
Type Parameters
TThe element type.