Repository navigation
OnixLabs.Core.Linq.IEnumerableExtensions
Provides LINQ-like extension methods for both non-generic IEnumerable and generic IEnumerable<T> sequences, including counting, iteration, hashing, predicate-based filtering, specification-based filtering, and collection string formatting.
| Namespace | OnixLabs.Core.Linq |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → IEnumerableExtensions
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IEnumerableExtensionsNote
IEnumerableExtensionsis decorated with[EditorBrowsable(EditorBrowsableState.Never)]. Although the class ispublic, it is hidden from IntelliSense completion lists so that only the extension methods themselves appear on their receiver types. Bring the extensions into scope withusing OnixLabs.Core.Linq;.Most methods use C# 14 extension blocks (
extension(IEnumerable? enumerable) { ... },extension<T>(IEnumerable<T>? enumerable) { ... }), so the receiver type appears in the block declaration rather than as athisparameter. A small set of methods are written as classicthis-parameter extensions (see Static (this-parameter) extensions). The signatures shown below are the call-site signatures as consumed by user code.
The methods are grouped by the receiver they extend: the non-generic IEnumerable, the generic IEnumerable<T>, an IEnumerable<T> group constrained to non-nullable element types (where T : notnull), and a set of classic this-parameter extensions.
Many methods accept a null receiver. Methods such as ForEach, GetContentHashCode, JoinToString, and ToCollectionString treat null as an empty sequence and return harmlessly. Methods that need to enumerate to produce a meaningful answer — for example Count, LongCount, IsEmpty, IsSingle, and the *OrNone family — throw ArgumentNullException via the Preconditions RequireNotNull guard.
The *OrNone query methods return Optional<T> and Result<Optional<T>> rather than nullable values, integrating cleanly with the library's option and result types. The Where(Specification<T>) and WhereNot(Specification<T>) overloads bridge to the Specification<T> pattern, evaluating the specification's IsSatisfiedBy predicate in memory (see IQueryableExtensions for the provider-translatable queryable counterpart).
| Method | Returns | Description |
|---|---|---|
Count() |
int |
Counts the elements of the sequence. |
LongCount() |
long |
Counts the elements of the sequence as a 64-bit value. |
ForEach(Action<object?>) |
void |
Performs an action for each element. |
GetContentHashCode() |
int |
Computes a content hash code from the elements. |
IsEmpty() |
bool |
Determines whether the sequence is empty. |
IsNotEmpty() |
bool |
Determines whether the sequence is not empty. |
IsSingle() |
bool |
Determines whether the sequence contains exactly one element. |
IsCountEven() |
bool |
Determines whether the element count is even. |
IsCountOdd() |
bool |
Determines whether the element count is odd. |
JoinToString(string) |
string |
Joins the elements into a single string. |
ToCollectionString() |
string |
Formats the sequence as a bracketed collection string. |
| Method | Returns | Description |
|---|---|---|
AllEqualBy<TProperty>(Func<T, TProperty>, IEqualityComparer<TProperty>?) |
bool |
Determines whether all elements are equal by a selected property. |
AnyEqualBy<TProperty>(Func<T, TProperty>, IEqualityComparer<TProperty>?) |
bool |
Determines whether any two elements are equal by a selected property. |
CountNot(Func<T, bool>) |
int |
Counts elements that do not satisfy a predicate. |
ForEach(Action<T>) |
void |
Performs an action for each element. |
GetContentHashCode() |
int |
Computes a content hash code from the elements. |
IsEmpty() |
bool |
Determines whether the sequence is empty. |
IsNotEmpty() |
bool |
Determines whether the sequence is not empty. |
IsSingle() |
bool |
Determines whether the sequence contains exactly one element. |
IsCountEven(Func<T, bool>?) |
bool |
Determines whether the count of matching elements is even. |
IsCountOdd(Func<T, bool>?) |
bool |
Determines whether the count of matching elements is odd. |
JoinToString(string) |
string |
Joins the elements into a single string. |
None(Func<T, bool>) |
bool |
Determines whether no element satisfies a predicate. |
SequenceEqualOrNull(IEnumerable<T>?, IEqualityComparer<T>?) |
bool |
Compares two sequences, treating two null sequences as equal. |
SumBy<TResult>(Func<T, TResult>) |
TResult |
Sums a selected numeric property of the elements. |
Where(Specification<T>) |
IEnumerable<T> |
Filters by a specification. |
WhereNot(Specification<T>) |
IEnumerable<T> |
Filters by a negated specification. |
WhereNot(Func<T, bool>) |
IEnumerable<T> |
Filters out elements that satisfy a predicate. |
ToCollectionString() |
string |
Formats the sequence as a bracketed collection string. |
| Method | Returns | Description |
|---|---|---|
FirstOrNone(Func<T, bool>?) |
Optional<T> |
Gets the first matching element, or None. |
LastOrNone(Func<T, bool>?) |
Optional<T> |
Gets the last matching element, or None. |
SingleOrNone(Func<T, bool>?) |
Result<Optional<T>> |
Gets the single matching element, None, or a failure. |
| Method | Returns | Description |
|---|---|---|
Sum<T>(this IEnumerable<T>) |
T |
Sums a sequence of numbers. |
WhereNotNull<T>(this IEnumerable<T?>) (reference) |
IEnumerable<T> |
Filters out null reference elements. |
WhereNotNull<T>(this IEnumerable<T?>) (value) |
IEnumerable<T> |
Filters out null nullable-value elements. |
ToCollectionString<T>(this IEnumerable<T>, string, IFormatProvider?) |
string |
Formats a formattable sequence as a collection string. |
using OnixLabs.Core;
using OnixLabs.Core.Linq;
using System.Collections.Generic;
IEnumerable<int> numbers = [1, 2, 3, 4];
bool single = numbers.IsSingle(); // False
int oddCount = numbers.CountNot(n => n % 2 == 0); // 2
int total = numbers.SumBy(n => n); // 10
string text = numbers.ToCollectionString(); // "[1, 2, 3, 4]"
Optional<int> firstEven = numbers.FirstOrNone(n => n % 2 == 0); // Some(2)
Result<Optional<int>> theFive = numbers.SingleOrNone(n => n == 5); // Some(None)public int Count()Obtains a number that represents how many elements are in the current IEnumerable.
Returns — The element count.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public long LongCount()Obtains a 64-bit number that represents how many elements are in the current IEnumerable.
Returns — The element count.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public void ForEach(Action<object?> action)Performs the specified action for each element of the current IEnumerable. If the sequence is null, the method returns without invoking action.
| Parameter | Type | Description |
|---|---|---|
action |
Action<object?> |
The action to perform for each element. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
action is null (and the sequence is non-null). |
public int GetContentHashCode()Gets the content hash code of the elements of the current IEnumerable. Returns 0 when the sequence is null.
Returns — The computed content hash code.
public bool IsEmpty()Determines whether the current IEnumerable is empty.
Returns — true if the sequence is empty; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool IsNotEmpty()Determines whether the current IEnumerable is not empty.
Returns — true if the sequence is not empty; otherwise, false.
public bool IsSingle()Determines whether the current IEnumerable contains a single element.
Returns — true if the sequence contains exactly one element; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool IsCountEven()Determines whether the current IEnumerable contains an even number of elements.
Returns — true if the element count is even; otherwise, false.
public bool IsCountOdd()Determines whether the current IEnumerable contains an odd number of elements.
Returns — true if the element count is odd; otherwise, false.
public string JoinToString(string separator = ", ")Joins the elements of the current IEnumerable into a new string. Returns string.Empty when the sequence is null. Each element is rendered via ToStringOrNull.
| Parameter | Type | Description |
|---|---|---|
separator |
string |
The separator that appears between joined elements. Defaults to ", ". |
Returns — The joined string.
public string ToCollectionString()Formats the current IEnumerable as a collection string, joining the elements and wrapping the result in [ and ]. Returns string.Empty when the sequence is null.
Returns — A string representing the sequence, for example [1, 2, 3].
public bool AllEqualBy<TProperty>(Func<T, TProperty> selector, IEqualityComparer<TProperty>? comparer = null)Determines whether all elements of the current IEnumerable<T> are equal by a specified property. An empty sequence returns true.
| Parameter | Type | Description |
|---|---|---|
selector |
Func<T, TProperty> |
The selector used to project the property from each element. |
comparer |
IEqualityComparer<TProperty>? |
The comparer used to compare projected values, or null to use the default comparer. |
| Type Parameter | Description |
|---|---|
TProperty |
The underlying type of each selected property. |
Returns — true if all selected element properties are equal; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or selector is null. |
public bool AnyEqualBy<TProperty>(Func<T, TProperty> selector, IEqualityComparer<TProperty>? comparer = null)Determines whether any element of the current IEnumerable<T> shares the same selected property value as the first element. An empty sequence returns false.
| Parameter | Type | Description |
|---|---|---|
selector |
Func<T, TProperty> |
The selector used to project the property from each element. |
comparer |
IEqualityComparer<TProperty>? |
The comparer used to compare projected values, or null to use the default comparer. |
| Type Parameter | Description |
|---|---|
TProperty |
The underlying type of each selected property. |
Returns — true if any selected element properties are equal; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or selector is null. |
public int CountNot(Func<T, bool> predicate)Obtains a number that represents how many elements in the current IEnumerable<T> do not satisfy the specified predicate.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool> |
The function to test each element for a condition. |
Returns — The number of elements that do not satisfy predicate.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or predicate is null. |
public void ForEach(Action<T> action)Performs the specified action for each element of the current IEnumerable<T>. If the sequence is null, the method returns without invoking action.
| Parameter | Type | Description |
|---|---|---|
action |
Action<T> |
The action to perform for each element. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
action is null (and the sequence is non-null). |
public int GetContentHashCode()Gets the content hash code of the elements of the current IEnumerable<T>. Returns 0 when the sequence is null.
Returns — The computed content hash code.
public bool IsEmpty()Determines whether the current IEnumerable<T> is empty.
Returns — true if the sequence is empty; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool IsNotEmpty()Determines whether the current IEnumerable<T> is not empty.
Returns — true if the sequence is not empty; otherwise, false.
public bool IsSingle()Determines whether the current IEnumerable<T> contains a single element.
Returns — true if the sequence contains exactly one element; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool IsCountEven(Func<T, bool>? predicate = null)Determines whether the current IEnumerable<T> contains an even number of elements that match the specified predicate, or in total when no predicate is supplied.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool>? |
An optional function to test each element. If null, all elements are counted. |
Returns — true if the matching element count is even; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool IsCountOdd(Func<T, bool>? predicate = null)Determines whether the current IEnumerable<T> contains an odd number of elements that match the specified predicate, or in total when no predicate is supplied.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool>? |
An optional function to test each element. If null, all elements are counted. |
Returns — true if the matching element count is odd; otherwise, false.
public string JoinToString(string separator = ", ")Joins the elements of the current IEnumerable<T> into a new string. Returns string.Empty when the sequence is null. Each element is rendered via ToStringOrNull.
| Parameter | Type | Description |
|---|---|---|
separator |
string |
The separator that appears between joined elements. Defaults to ", ". |
Returns — The joined string.
public bool None(Func<T, bool> predicate)Determines whether none of the elements of the current IEnumerable<T> satisfy the specified predicate.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool> |
The function to test each element for a condition. |
Returns — true if no element satisfies predicate; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public bool SequenceEqualOrNull(IEnumerable<T>? other, IEqualityComparer<T>? comparer = null)Determines whether two sequences are both null, or equal by comparing their elements using a specified comparer. If exactly one sequence is null, the result is false.
| Parameter | Type | Description |
|---|---|---|
other |
IEnumerable<T>? |
The sequence to compare to the current sequence. |
comparer |
IEqualityComparer<T>? |
The comparer used to compare elements, or null to use the default comparer. |
Returns — true if both sequences are null, or of equal length with corresponding elements comparing equal; otherwise, false.
public TResult SumBy<TResult>(Func<T, TResult> selector) where TResult : INumberBase<TResult>Calculates the sum of a selected numeric property of the elements of the current IEnumerable<T>.
| Parameter | Type | Description |
|---|---|---|
selector |
Func<T, TResult> |
The selector used to project the numeric property from each element. |
| Type Parameter | Description |
|---|---|
TResult |
The INumberBase<TResult> type of each element to sum. |
Returns — The sum of the selected properties.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or selector is null. |
public IEnumerable<T> Where(Specification<T> specification)Filters a sequence of values based on a Specification<T>.
| Parameter | Type | Description |
|---|---|---|
specification |
Specification<T> |
The specification to filter by. |
Returns — An IEnumerable<T> containing elements that satisfy the specification.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or specification is null. |
public IEnumerable<T> WhereNot(Specification<T> specification)Filters a sequence of values based on a negated Specification<T>.
| Parameter | Type | Description |
|---|---|---|
specification |
Specification<T> |
The specification to filter by. |
Returns — An IEnumerable<T> containing elements that satisfy the negated specification.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or specification is null. |
public IEnumerable<T> WhereNot(Func<T, bool> predicate)Filters the current IEnumerable<T>, yielding only elements that do not satisfy the specified predicate. The result is lazily evaluated.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool> |
The function to test each element for a condition. |
Returns — A new IEnumerable<T> containing elements that do not satisfy the condition.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence or predicate is null. |
public string ToCollectionString()Formats the current IEnumerable<T> as a collection string, joining the elements and wrapping the result in [ and ]. Returns string.Empty when the sequence is null.
Returns — A string representing the sequence, for example [1, 2, 3].
public Optional<T> FirstOrNone(Func<T, bool>? predicate = null)Obtains the first element of the current IEnumerable<T> that satisfies the specified predicate; otherwise Optional<T>.None if no such element is found. When predicate is null, the first element of the sequence is returned.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool>? |
An optional function to test each element for a condition. |
| Type Parameter | Description |
|---|---|
T |
The non-nullable element type (where T : notnull). |
Returns — The first matching element wrapped in an Optional<T>, or Optional<T>.None.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public Optional<T> LastOrNone(Func<T, bool>? predicate = null)Obtains the last element of the current IEnumerable<T> that satisfies the specified predicate; otherwise Optional<T>.None if no such element is found. When predicate is null, the last element of the sequence is returned.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool>? |
An optional function to test each element for a condition. |
| Type Parameter | Description |
|---|---|
T |
The non-nullable element type (where T : notnull). |
Returns — The last matching element wrapped in an Optional<T>, or Optional<T>.None.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public Result<Optional<T>> SingleOrNone(Func<T, bool>? predicate = null)Obtains a single element of the current IEnumerable<T> that satisfies the specified predicate; otherwise Optional<T>.None if no such element is found, or a Failure<T> wrapping an InvalidOperationException if the sequence contains more than one matching element. When predicate is null, all elements are considered.
| Parameter | Type | Description |
|---|---|---|
predicate |
Func<T, bool>? |
An optional function to test each element for a condition. |
| Type Parameter | Description |
|---|---|
T |
The non-nullable element type (where T : notnull). |
Returns — A Result<Optional<T>>: a success containing the single matching element (or None), or a failure wrapping InvalidOperationException when more than one element matches.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
The current sequence is null. |
public static T Sum<T>(this IEnumerable<T> enumerable) where T : INumberBase<T>Calculates the sum of the elements of the current IEnumerable<T>, starting from T.Zero.
| Parameter | Type | Description |
|---|---|---|
enumerable |
IEnumerable<T> |
The sequence to sum. |
| Type Parameter | Description |
|---|---|
T |
The INumberBase<T> element type of the sequence. |
Returns — The sum of the elements.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
enumerable is null. |
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : classFilters the current sequence, returning only elements that are not null.
| Parameter | Type | Description |
|---|---|---|
enumerable |
IEnumerable<T?> |
The sequence on which to perform the operation. |
| Type Parameter | Description |
|---|---|
T |
The reference element type of the sequence (where T : class). |
Returns — A new IEnumerable<T> containing the non-null elements.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
enumerable is null. |
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : structFilters the current sequence of nullable value types, returning the underlying values of elements that have a value.
| Parameter | Type | Description |
|---|---|---|
enumerable |
IEnumerable<T?> |
The sequence on which to perform the operation. |
| Type Parameter | Description |
|---|---|
T |
The value element type of the sequence (where T : struct). |
Returns — A new IEnumerable<T> containing the values of elements that are not null.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
enumerable is null. |
public static string ToCollectionString<T>(
this IEnumerable<T> enumerable, string format, IFormatProvider? provider = null) where T : IFormattableFormats the current IEnumerable<T> as a collection string, applying the specified format to each element before joining and wrapping the result in [ and ].
| Parameter | Type | Description |
|---|---|---|
enumerable |
IEnumerable<T> |
The sequence to format. |
format |
string |
The format applied to each element. |
provider |
IFormatProvider? |
An object that supplies culture-specific formatting information, or null. |
| Type Parameter | Description |
|---|---|
T |
The IFormattable element type of the sequence. |
Returns — A string representing the sequence.
Exceptions
| Exception | Condition |
|---|---|
ArgumentNullException |
enumerable is null. |
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions