Table of Contents

Class EnumerableExtensions

Namespace
ChartTools.Extensions.Linq
Assembly
ChartTools.dll

Provides a set of extension methods for IEnumerable<T>.

public static class EnumerableExtensions
Inheritance
EnumerableExtensions
Inherited Members

Methods

AlternateBy<T, TKey>(IEnumerable<IEnumerable<T>>, Func<T, TKey>)

Combines enumerables by alternating between each source for every item based on a key.

public static IEnumerable<T> AlternateBy<T, TKey>(this IEnumerable<IEnumerable<T>> sources, Func<T, TKey> selector) where TKey : IComparable<TKey>

Parameters

sources IEnumerable<IEnumerable<T>>
Set of enumerables to alternate between
selector Func<T, TKey>

Selector function returning the alternate key from an item

Returns

IEnumerable<T>

Combined items from all enumerables, taking the next item with the smallest key from each enumerable.

Type Parameters

T

Type of root items

TKey

Type of key used to compare items when alternating

Remarks

When the end of an enumerable is reached, alternating continues while skipping that enumerable until all are finished.

Alternate<T>(IEnumerable<IEnumerable<T>>)

Combines enumerables by alternating between each source for every item.

public static IEnumerable<T> Alternate<T>(this IEnumerable<IEnumerable<T>> sources)

Parameters

sources IEnumerable<IEnumerable<T>>

Set of enumerables to alternate between

Returns

IEnumerable<T>

Combined items from all enumerables, taking one item from each before looping.

Type Parameters

T

Type of root items

Remarks

When the end of an enumerable is reached, alternating continues while skipping that enumerable until all are finished.

Distinct<T>(IEnumerable<T>, EqualityComparison<T?>)

Returns distinct elements of a collection using a method to determine the equality of elements.

public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, EqualityComparison<T?> comparison)

Parameters

source IEnumerable<T>

Collection to get distinct items from

comparison EqualityComparison<T>

Method determining if two elements are the same

Returns

IEnumerable<T>

Type Parameters

T

Type of items in the collection

FirstOrDefault<T>(IEnumerable<T>, Predicate<T>, T?, out bool)

Gets the first item that meets a condition from a collection or a default value if no such item was found.

public static T? FirstOrDefault<T>(this IEnumerable<T> source, Predicate<T> predicate, T? defaultValue, out bool returnedDefault)

Parameters

source IEnumerable<T>

Collection to get the first item of

predicate Predicate<T>

Condition to compare items against

defaultValue T

Value to return if no items meeting the condition were found

returnedDefault bool

true if no items meeting the condition were found

Returns

T

Type Parameters

T

Type of items in the collection

NonNull<T>(IEnumerable<T?>)

Excludes null item from a set of nullable values.

public static IEnumerable<T> NonNull<T>(this IEnumerable<T?> source) where T : struct

Parameters

source IEnumerable<T?>

Set of values wrapped in Nullable<T>

Returns

IEnumerable<T>

Set of the nullable values unwrapped to the underlying type with null items excluded.

Type Parameters

T

Underlying value type

NonNull<T>(IEnumerable<T?>)

Excludes null items from a set of references.

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

Parameters

source IEnumerable<T>

Returns

IEnumerable<T>

Type Parameters

T

Type of items of references types or boxed values

ReplaceSection<T>(IEnumerable<T>, SectionReplacement<T>)

Replaces a section from a collection with other items.

public static IEnumerable<T> ReplaceSection<T>(this IEnumerable<T> source, SectionReplacement<T> replacement)

Parameters

source IEnumerable<T>

Items to replace a section in

replacement SectionReplacement<T>

Set of rules defining the replacement

Returns

IEnumerable<T>

Items with the specified section replaced

Type Parameters

T

Type of items in the collection

Remarks

Items that match StartReplace or EndReplace are not included in the output.

ReplaceSections<T>(IEnumerable<T>, IEnumerable<SectionReplacement<T>>)

Replaces multiple sections of items from a collection.

public static IEnumerable<T> ReplaceSections<T>(this IEnumerable<T> source, IEnumerable<SectionReplacement<T>> replacements)

Parameters

source IEnumerable<T>

Items to replace sections in

replacements IEnumerable<SectionReplacement<T>>

Set of definitions of section replacements

Returns

IEnumerable<T>

Items with the specified section replaced

Type Parameters

T

Type of items in the collection

Remarks

Items that match StartReplace or EndReplace are not included in the output.

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

Replaces items that meet a condition with another item.

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

Parameters

source IEnumerable<T>

The collection to replace the items of

predicate Predicate<T>

A function that determines if an item must be replaced

replacement T

The item to replace items with

Returns

IEnumerable<T>

Type Parameters

T

Type of items in the collection

TryGetFirstOfType<TResult>(IEnumerable, out TResult)

Tries to get the first item of a given type in a collection.

public static bool TryGetFirstOfType<TResult>(this IEnumerable source, out TResult result)

Parameters

source IEnumerable

Source of items

result TResult

Found item

Returns

bool

true if an item was found

Type Parameters

TResult

Type to get items of

TryGetFirst<T>(IEnumerable<T>, Predicate<T>, out T?)

Tries to get the first item that meets a condition from a collection.

public static bool TryGetFirst<T>(this IEnumerable<T> source, Predicate<T> predicate, out T? item) where T : notnull

Parameters

source IEnumerable<T>

Collection to get the first item of

predicate Predicate<T>

Method that returns true if a given item meets the condition

item T

Found item

Returns

bool

true if an item was found

Type Parameters

T

Type of items in the collection

TryGetFirst<T>(IEnumerable<T>, out T)

Tries to get the first element of a collection.

public static bool TryGetFirst<T>(this IEnumerable<T> source, out T result)

Parameters

source IEnumerable<T>

Source of items

result T

Found item

Returns

bool

true if an item was found

Type Parameters

T

Type of items in the collection