Table of Contents

Class MoreLinqExtensions

Namespace
Garyon.Extensions
Assembly
Garyon.dll

Provides more LINQ-like extensions that are not that likely to be considered candidates for being imported into System.Linq.

public static class MoreLinqExtensions
Inheritance
MoreLinqExtensions
Inherited Members

Methods

AnyDeep<T>(IEnumerable<IEnumerable<T>>)

Determines whether there are any elements contained in any sequence of the sequences that are provided.

public static bool AnyDeep<T>(this IEnumerable<IEnumerable<T>> source)

Parameters

source IEnumerable<IEnumerable<T>>

The sequence of sequences to analyze on whether any elements are contained.

Returns

bool

true if there is at least one element in any of the sequences, otherwise false.

Type Parameters

T

The type of elements stored in the sequences.

Apply<TSource>(TSource, Action<TSource>)

Applies a function to the given value and returns the same value.

public static TSource Apply<TSource>(this TSource source, Action<TSource> function)

Parameters

source TSource

The source value.

function Action<TSource>

The function to apply to the source value.

Returns

TSource

The same source value for chaining purposes.

Type Parameters

TSource

The type of the source value.

Apply<TSource, TResult>(TSource, Func<TSource, TResult>)

Applies a function to the given value and returns the result.

public static TResult Apply<TSource, TResult>(this TSource source, Func<TSource, TResult> function)

Parameters

source TSource

The source value.

function Func<TSource, TResult>

The function to apply to the source value.

Returns

TResult

The resulting value after applying function to source.

Type Parameters

TSource

The type of the source value.

TResult

The type of the result value.

CountAtLeast<T>(IEnumerable<T>, Func<T, bool>, int)

Determines whether there are at least a specified number of occurrences of elements meeting a condition in a collection.

public static bool CountAtLeast<T>(this IEnumerable<T> source, Func<T, bool> predicate, int occurrences)

Parameters

source IEnumerable<T>

The source collection whose elements to compare against the condition.

predicate Func<T, bool>

The condition that the elements must meet.

occurrences int

The minimum number of elements that must meet the condition.

Returns

bool

true if the provided collection contains at least occurrences elements that meet the given condition, otherwise false.

Type Parameters

T

The type of the elements in the collection.

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

Determines whether there are at least a specified number of occurrences of elements in an enumerable.

public static bool CountAtLeast<T>(this IEnumerable<T> source, int minCount)

Parameters

source IEnumerable<T>
minCount int

The minimum number of elements that must exist in the enumerable.

Returns

bool

true if the provided collection contains at least minCount elements, otherwise false.

Type Parameters

T

CountAtMost<T>(IEnumerable<T>, Func<T, bool>, int)

Determines whether there are at most a specified number of occurrences of elements meeting a condition in a collection.

public static bool CountAtMost<T>(this IEnumerable<T> source, Func<T, bool> predicate, int occurrences)

Parameters

source IEnumerable<T>

The source collection whose elements to compare against the condition.

predicate Func<T, bool>

The condition that the elements must meet.

occurrences int

The minimum number of elements that must meet the condition.

Returns

bool

true if the provided collection contains at least occurrences elements that meet the given condition, otherwise false.

Type Parameters

T

The type of the elements in the collection.

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

Determines whether there are at most a specified number of occurrences of elements in an enumerable.

public static bool CountAtMost<T>(this IEnumerable<T> source, int maxCount)

Parameters

source IEnumerable<T>
maxCount int

The maximum number of elements that must exist in the enumerable.

Returns

bool

true if the provided collection contains at most maxCount elements, otherwise false.

Type Parameters

T

CountBetween<T>(IEnumerable<T>, Func<T, bool>, int, int)

Determines whether there are between a specified range (inclusive) of occurrences of elements meeting a condition in a collection.

public static bool CountBetween<T>(this IEnumerable<T> source, Func<T, bool> predicate, int minOccurrences, int maxOccurrences)

Parameters

source IEnumerable<T>

The source collection whose elements to compare against the condition.

predicate Func<T, bool>

The condition that the elements must meet.

minOccurrences int

The minimum number of elements that must meet the given condition.

maxOccurrences int

The maximum number of elements that may meet the given condition.

Returns

bool

true if the provided collection contains between minOccurrences and maxOccurrences (inclusive) elements that meet the given condition, otherwise false.

Type Parameters

T

The type of the elements in the collection.

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

Determines whether there are between a specified range (inclusive) of occurrences of elements in an enumerable.

public static bool CountBetween<T>(this IEnumerable<T> source, int minCount, int maxCount)

Parameters

source IEnumerable<T>
minCount int

The minimum number of elements that must exist in the enumerable.

maxCount int

The maximum number of elements that may exist in the enumerable.

Returns

bool

true if the provided collection contains between minCount and maxCount (inclusive) elements, otherwise false.

Type Parameters

T

CountExactly<T>(IEnumerable<T>, Func<T, bool>, int)

Determines whether there are exactly a specified number of occurrences of elements meeting a condition in a collection.

public static bool CountExactly<T>(this IEnumerable<T> source, Func<T, bool> predicate, int occurrences)

Parameters

source IEnumerable<T>

The source collection whose elements to compare against the condition.

predicate Func<T, bool>

The condition that the elements must meet.

occurrences int

The exact number of elements that must meet the given condition.

Returns

bool

true if the provided collection contains exactly occurrences elements that meet the given condition, otherwise false.

Type Parameters

T

The type of the elements in the collection.

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

Determines whether there are exactly a specified number of occurrences of elements in an enumerable.

public static bool CountExactly<T>(this IEnumerable<T> source, int count)

Parameters

source IEnumerable<T>
count int

The exact number of elements that must exist in the enumerable.

Returns

bool

true if the provided collection contains exactly count elements, otherwise false.

Type Parameters

T

EnumeratePerformAction<T>(IEnumerable<T>, Action<T>?, Action<T>?, Action<T>)

Performs an action for each element in a collection, while also performing an additional specified action for the first and the last elements.

public static bool EnumeratePerformAction<T>(this IEnumerable<T> source, Action<T>? firstElementAction, Action<T>? lastElementAction, Action<T> enumerationAction)

Parameters

source IEnumerable<T>

The source collection whose elements to perform the actions on. If the collection is empty, no action will be taken.

firstElementAction Action<T>

The action to perform on the first element.

lastElementAction Action<T>

The action to perform on the last element.

enumerationAction Action<T>

The action to perform on all enumerated elements. This is also applied to the first and the last elements.

Returns

bool

true if source contained at least one element and actions were performed, otherwise false.

Type Parameters

T

The type of the elements in the collection.

NotNullCount<T>(IEnumerable<T>)

Gets the count of elements that are not null.

public static int NotNullCount<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The IEnumerable<T> whose elements to iterate.

Returns

int

The total number of elements that are not null.

Type Parameters

T

The type of the elements stored in the IEnumerable<T>.

NullCount<T>(IEnumerable<T>)

Gets the count of elements that are not null.

public static int NullCount<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The IEnumerable<T> whose elements to iterate.

Returns

int

The total number of elements that are not null.

Type Parameters

T

The type of the elements stored in the IEnumerable<T>.

OnlyOrDefault<T>(IEnumerable<T>?)

Gets the only element of the sequence, if it only has one element, otherwise returns default.

public static T? OnlyOrDefault<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source sequence.

Returns

T

The only element of the sequence, or default.

Type Parameters

T

The type of the elements in the sequence.

SelectOrDefault<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>?)

Selects the enumerable's items based on a selector. If the selector is null, a null enumerable is returned instead.

public static IEnumerable<TResult>? SelectOrDefault<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult>? selector)

Parameters

source IEnumerable<TSource>
selector Func<TSource, TResult>

Returns

IEnumerable<TResult>

Type Parameters

TSource
TResult

ToListDictionary<TKey, TValue>(IEnumerable<IGrouping<TKey, TValue>>)

Converts all groupings to a dictionary, with the values being a List<T>.

public static Dictionary<TKey, List<TValue>> ToListDictionary<TKey, TValue>(this IEnumerable<IGrouping<TKey, TValue>> groupings) where TKey : notnull

Parameters

groupings IEnumerable<IGrouping<TKey, TValue>>

Returns

Dictionary<TKey, List<TValue>>

Type Parameters

TKey
TValue

WhereNotNullKeys<TKey, TValue>(IEnumerable<IGrouping<TKey?, TValue>>)

Filters all groupings and returns only those that have non-null keys.

public static IEnumerable<IGrouping<TKey, TValue>> WhereNotNullKeys<TKey, TValue>(this IEnumerable<IGrouping<TKey?, TValue>> grouping)

Parameters

grouping IEnumerable<IGrouping<TKey, TValue>>

Returns

IEnumerable<IGrouping<TKey, TValue>>

Type Parameters

TKey
TValue

WhereNotNull<T>(IEnumerable<T?>)

Filters the items and returns only those that are not null.

public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)

Parameters

source IEnumerable<T>

Returns

IEnumerable<T>

Type Parameters

T

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

Checks all elements against a predicate and returns only those elements that do NOT meet the condition.

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

Parameters

source IEnumerable<T>

The sequence of elements to filter.

predicate Func<T, bool>

A function to test each element for a condition. Elements for which this function returns false are included in the result.

Returns

IEnumerable<T>

Type Parameters

T

WhereOrSource<T>(IEnumerable<T>, Func<T, bool>?)

Filters out the contents of the enumerable, if a predicate is provided. Otherwise, returns the source enumerable as-is.

public static IEnumerable<T> WhereOrSource<T>(this IEnumerable<T> source, Func<T, bool>? predicate)

Parameters

source IEnumerable<T>
predicate Func<T, bool>

Returns

IEnumerable<T>

Type Parameters

T

WherePredicate<T>(IEnumerable<T>, Predicate<T>)

Filters a sequence of values based on a predicate.

public static IEnumerable<T> WherePredicate<T>(this IEnumerable<T> source, Predicate<T> predicate)

Parameters

source IEnumerable<T>

An IEnumerable<T> to filter.

predicate Predicate<T>

A function to test each element for a condition.

Returns

IEnumerable<T>

An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.

Type Parameters

T

Exceptions

ArgumentNullException

source or predicate is null.

Zip<TA, TB>(IEnumerable<TA>, IEnumerable<TB>)

Zips both sequences into a sequence of tuples.

public static IEnumerable<(TA First, TB Second)> Zip<TA, TB>(this IEnumerable<TA> source, IEnumerable<TB> other)

Parameters

source IEnumerable<TA>
other IEnumerable<TB>

Returns

IEnumerable<(TA First, TB Second)>

Type Parameters

TA
TB