Class EnumerableExtensions
- Namespace
- Plugin.BaseTypeExtensions
- Assembly
- Plugin.BaseTypeExtensions.dll
Provides extension methods for working with IEnumerable and IEnumerable<T>.
public static class EnumerableExtensions
- Inheritance
-
EnumerableExtensions
- Inherited Members
Methods
Enqueue<T>(ConcurrentQueue<T>, T, int)
Enqueues an item to the queue and ensures the queue does not exceed the specified maximum size.
public static void Enqueue<T>(this ConcurrentQueue<T> queue, T obj, int max)
Parameters
queueConcurrentQueue<T>The queue to enqueue the item to. Cannot be null.
objTThe item to enqueue. Can be null.
maxintThe maximum size of the queue.
Type Parameters
T
GetOrDefault<T>(IEnumerable<T?>?, int, T?)
Gets the element at the specified index or returns the default value if the index is out of range.
public static T? GetOrDefault<T>(this IEnumerable<T?>? enumerable, int index, T? defaultValue = default)
Parameters
enumerableIEnumerable<T>The enumerable to get the element from. Can be null.
indexintThe index of the element to get. If the index is out of range or if the enumerable is null, the default value is returned.
defaultValueTThe default value to return if the index is out of range. Can be null.
Returns
- T
Type Parameters
T
NullIfEmpty(IEnumerable?)
Coalesces the input to null if it is empty.
public static IEnumerable? NullIfEmpty(this IEnumerable? input)
Parameters
inputIEnumerableThe input sequence to coalesce. Can be null.
Returns
- IEnumerable
The input sequence that was passed in if it is not empty; otherwise, null.
NullIfEmpty<T>(IEnumerable<T?>?)
Coalesces the input to null if it is empty.
public static IEnumerable<T?>? NullIfEmpty<T>(this IEnumerable<T?>? input)
Parameters
inputIEnumerable<T>The input sequence to coalesce. Can be null.
Returns
- IEnumerable<T>
The input sequence that was passed in if it is not empty; otherwise, null.
Type Parameters
T
PickRandom<T>(IEnumerable<T>)
Picks a random element from the collection.
public static T PickRandom<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- T
Type Parameters
T
Exceptions
- InvalidOperationException
Thrown if the source collection is empty.
PickRandom<T>(IEnumerable<T>, int)
Picks a specified number of random elements from the collection.
public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
Parameters
sourceIEnumerable<T>countint
Returns
- IEnumerable<T>
Type Parameters
T
Exceptions
- ArgumentException
Thrown if requested count exceeds the source collection count.
Shuffle<T>(IEnumerable<T>)
Shuffles the elements of the collection using Fisher-Yates algorithm.
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
UpdateFromAsync<TAdd, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TRemove>, Func<TAdd, CancellationToken, ValueTask>, Func<TRemove, CancellationToken, ValueTask>, CancellationToken)
Asynchronously updates the output collection by adding and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.
public static ValueTask UpdateFromAsync<TAdd, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TRemove> removedItems, Func<TAdd, CancellationToken, ValueTask> addAction, Func<TRemove, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TRemove>The collection to update.
addedItemsIEnumerable<TAdd>The items to add to the collection.
removedItemsIEnumerable<TRemove>The items to remove from the collection.
addActionFunc<TAdd, CancellationToken, ValueTask>Async action to perform when adding an item. This parameter is required.
removeActionFunc<TRemove, CancellationToken, ValueTask>Async action to perform when removing an item. This parameter is required.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Type Parameters
TAddThe type of items to add.
TRemoveThe type of items to remove.
UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, ValueTask>, Func<TOutput, CancellationToken, ValueTask>, CancellationToken)
Asynchronously updates the output collection from the input collection with item comparison and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.
public static ValueTask UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, CancellationToken, ValueTask> addAction, Func<TOutput, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TOutput>The collection to update.
inputIEnumerable<TInput>The collection to use as source for updates.
areRepresentingTheSameItemFunc<TInput, TOutput, bool>Function to determine if an input item and output item represent the same entity.
addActionFunc<TInput, CancellationToken, ValueTask>Async action to perform when adding an item.
removeActionFunc<TOutput, CancellationToken, ValueTask>Async action to perform when removing an item.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Type Parameters
TInputThe type of the input items.
TOutputThe type of the output items.
UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, ValueTask>, Func<TOutput, TInput, CancellationToken, ValueTask>, Func<TOutput, CancellationToken, ValueTask>, CancellationToken)
Asynchronously updates the output collection from the input collection with item comparison and actions for adding, updating, and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.
public static ValueTask UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput, bool> areRepresentingTheSameValue, Func<TInput, CancellationToken, ValueTask> addAction, Func<TOutput, TInput, CancellationToken, ValueTask> updateAction, Func<TOutput, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TOutput>The collection to update.
inputIEnumerable<TInput>The collection to use as source for updates.
areRepresentingTheSameItemFunc<TInput, TOutput, bool>Function to determine if an input item and output item represent the same entity.
areRepresentingTheSameValueFunc<TInput, TOutput, bool>Function to determine if an input item and output item have the same value.
addActionFunc<TInput, CancellationToken, ValueTask>Async action to perform when adding an item.
updateActionFunc<TOutput, TInput, CancellationToken, ValueTask>Async action to perform when updating an item.
removeActionFunc<TOutput, CancellationToken, ValueTask>Async action to perform when removing an item.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Type Parameters
TInputThe type of the input items.
TOutputThe type of the output items.
UpdateFromAsync<TAdd, TUpdate, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TUpdate>, IEnumerable<TRemove>, Func<TAdd, CancellationToken, ValueTask>, Func<TUpdate, CancellationToken, ValueTask>, Func<TRemove, CancellationToken, ValueTask>, CancellationToken)
Asynchronously updates the output collection by adding, updating, and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.
public static ValueTask UpdateFromAsync<TAdd, TUpdate, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TUpdate> updatedItems, IEnumerable<TRemove> removedItems, Func<TAdd, CancellationToken, ValueTask> addAction, Func<TUpdate, CancellationToken, ValueTask> updateAction, Func<TRemove, CancellationToken, ValueTask> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TRemove>The collection to update.
addedItemsIEnumerable<TAdd>The items to add to the collection.
updatedItemsIEnumerable<TUpdate>The items to update in the collection.
removedItemsIEnumerable<TRemove>The items to remove from the collection.
addActionFunc<TAdd, CancellationToken, ValueTask>Async action to perform when adding an item. This parameter is required.
updateActionFunc<TUpdate, CancellationToken, ValueTask>Async action to perform when updating an item. This parameter is required.
removeActionFunc<TRemove, CancellationToken, ValueTask>Async action to perform when removing an item. This parameter is required.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Type Parameters
TAddThe type of items to add.
TUpdateThe type of items to update.
TRemoveThe type of items to remove.
UpdateFrom<TAdd, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TRemove>, Action<TAdd>, Action<TRemove>)
Updates the output collection by adding and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.
public static void UpdateFrom<TAdd, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TRemove> removedItems, Action<TAdd> addAction, Action<TRemove> removeAction)
Parameters
outputIEnumerable<TRemove>The collection to update.
addedItemsIEnumerable<TAdd>The items to add to the collection.
removedItemsIEnumerable<TRemove>The items to remove from the collection.
addActionAction<TAdd>Action to perform when adding an item. This parameter is required.
removeActionAction<TRemove>Action to perform when removing an item. This parameter is required.
Type Parameters
TAddThe type of items to add.
TRemoveThe type of items to remove.
UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Action<TInput>, Action<TOutput>)
Updates the output collection from the input collection with item comparison and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.
public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Action<TInput> addAction, Action<TOutput> removeAction)
Parameters
outputIEnumerable<TOutput>The collection to update.
inputIEnumerable<TInput>The collection to use as source for updates.
areRepresentingTheSameItemFunc<TInput, TOutput, bool>Function to determine if an input item and output item represent the same entity.
addActionAction<TInput>Action to perform when adding an item.
removeActionAction<TOutput>Action to perform when removing an item.
Type Parameters
TInputThe type of the input items.
TOutputThe type of the output items.
UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput, bool>, Action<TInput>, Action<TOutput, TInput>, Action<TOutput>)
Updates the output collection from the input collection with item comparison and actions for adding, updating, and removing items. This is a Layer 2 (diff-calculating) method that calculates differences and delegates to Layer 1.
public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput, bool> areRepresentingTheSameValue, Action<TInput> addAction, Action<TOutput, TInput> updateAction, Action<TOutput> removeAction)
Parameters
outputIEnumerable<TOutput>The collection to update.
inputIEnumerable<TInput>The collection to use as source for updates.
areRepresentingTheSameItemFunc<TInput, TOutput, bool>Function to determine if an input item and output item represent the same entity.
areRepresentingTheSameValueFunc<TInput, TOutput, bool>Function to determine if an input item and output item have the same value.
addActionAction<TInput>Action to perform when adding an item.
updateActionAction<TOutput, TInput>Action to perform when updating an item.
removeActionAction<TOutput>Action to perform when removing an item.
Type Parameters
TInputThe type of the input items.
TOutputThe type of the output items.
UpdateFrom<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput>, Action<TOutput>, Action<TOutput>)
Updates the output collection from the input collection with item comparison, type conversion, and actions for adding and removing items. This is a Layer 2 (diff-calculating) method that calculates differences, converts types, and delegates to Layer 1.
public static void UpdateFrom<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput> fromInputTypeToOutputTypeConversion, Action<TOutput> addAction, Action<TOutput> removeAction)
Parameters
outputIEnumerable<TOutput>The collection to update.
inputIEnumerable<TInput>The collection to use as source for updates.
areRepresentingTheSameItemFunc<TInput, TOutput, bool>Function to determine if an input item and output item represent the same entity.
fromInputTypeToOutputTypeConversionFunc<TInput, TOutput>Function to convert input items to output items.
addActionAction<TOutput>Action to perform when adding an item.
removeActionAction<TOutput>Action to perform when removing an item.
Type Parameters
TInputThe type of the input items.
TOutputThe type of the output items.
UpdateFrom<TAdd, TUpdate, TRemove>(IEnumerable<TRemove>, IEnumerable<TAdd>, IEnumerable<TUpdate>, IEnumerable<TRemove>, Action<TAdd>, Action<TUpdate>, Action<TRemove>)
Updates the output collection by adding, updating, and removing specified items. This is a Layer 1 (core) method that executes actions on explicit item lists.
public static void UpdateFrom<TAdd, TUpdate, TRemove>(this IEnumerable<TRemove> output, IEnumerable<TAdd> addedItems, IEnumerable<TUpdate> updatedItems, IEnumerable<TRemove> removedItems, Action<TAdd> addAction, Action<TUpdate> updateAction, Action<TRemove> removeAction)
Parameters
outputIEnumerable<TRemove>The collection to update.
addedItemsIEnumerable<TAdd>The items to add to the collection.
updatedItemsIEnumerable<TUpdate>The items to update in the collection.
removedItemsIEnumerable<TRemove>The items to remove from the collection.
addActionAction<TAdd>Action to perform when adding an item. This parameter is required.
updateActionAction<TUpdate>Action to perform when updating an item. This parameter is required.
removeActionAction<TRemove>Action to perform when removing an item. This parameter is required.
Type Parameters
TAddThe type of items to add.
TUpdateThe type of items to update.
TRemoveThe type of items to remove.