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<T>(IEnumerable<T>, IEnumerable<T>?, IEnumerable<T>?, Func<T, CancellationToken, Task>?, Func<T, CancellationToken, Task>?, CancellationToken)
Asynchronously updates the output collection by adding and removing specified items.
public static Task UpdateFromAsync<T>(this IEnumerable<T> output, IEnumerable<T>? addedItems = null, IEnumerable<T>? removedItems = null, Func<T, CancellationToken, Task>? addAction = null, Func<T, CancellationToken, Task>? removeAction = null, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<T>The collection to update.
addedItemsIEnumerable<T>The items to add to the collection.
removedItemsIEnumerable<T>The items to remove from the collection.
addActionFunc<T, CancellationToken, Task>Async action to perform when adding an item.
removeActionFunc<T, CancellationToken, Task>Async action to perform when removing an item.
cancellationTokenCancellationTokenToken to monitor for cancellation requests.
Returns
Type Parameters
TThe type of the items.
UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, Task>, Func<TOutput, CancellationToken, Task>, CancellationToken)
Asynchronously updates the output collection from the input collection with item comparison and actions for adding and removing items.
public static Task UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, CancellationToken, Task> addAction, Func<TOutput, CancellationToken, Task> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TOutput>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>addActionFunc<TInput, CancellationToken, Task>removeActionFunc<TOutput, CancellationToken, Task>cancellationTokenCancellationToken
Returns
Type Parameters
TInputTOutput
UpdateFromAsync<TInput, TOutput>(IEnumerable<TOutput>, IEnumerable<TInput>, Func<TInput, TOutput, bool>, Func<TInput, TOutput, bool>, Func<TInput, CancellationToken, Task>, Func<TOutput, TInput, CancellationToken, Task>, Func<TOutput, CancellationToken, Task>, CancellationToken)
Asynchronously updates the output collection from the input collection with item comparison and actions for adding, updating, and removing items.
public static Task UpdateFromAsync<TInput, TOutput>(this IEnumerable<TOutput> output, IEnumerable<TInput> input, Func<TInput, TOutput, bool> areRepresentingTheSameItem, Func<TInput, TOutput, bool> areRepresentingTheSameValue, Func<TInput, CancellationToken, Task> addAction, Func<TOutput, TInput, CancellationToken, Task> updateAction, Func<TOutput, CancellationToken, Task> removeAction, CancellationToken cancellationToken = default)
Parameters
outputIEnumerable<TOutput>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>areRepresentingTheSameValueFunc<TInput, TOutput, bool>addActionFunc<TInput, CancellationToken, Task>updateActionFunc<TOutput, TInput, CancellationToken, Task>removeActionFunc<TOutput, CancellationToken, Task>cancellationTokenCancellationToken
Returns
Type Parameters
TInputTOutput
UpdateFrom<T>(IEnumerable<T>, IEnumerable<T>?, IEnumerable<T>?, Action<T>?, Action<T>?)
Updates the output collection by adding and removing specified items.
public static void UpdateFrom<T>(this IEnumerable<T> output, IEnumerable<T>? addedItems = null, IEnumerable<T>? removedItems = null, Action<T>? addAction = null, Action<T>? removeAction = null)
Parameters
outputIEnumerable<T>The collection to update.
addedItemsIEnumerable<T>The items to add to the collection.
removedItemsIEnumerable<T>The items to remove from the collection.
addActionAction<T>Action to perform when adding an item.
removeActionAction<T>Action to perform when removing an item.
Type Parameters
TThe type of the items.
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.
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>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>addActionAction<TInput>removeActionAction<TOutput>
Type Parameters
TInputTOutput
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.
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>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>areRepresentingTheSameValueFunc<TInput, TOutput, bool>addActionAction<TInput>updateActionAction<TOutput, TInput>removeActionAction<TOutput>
Type Parameters
TInputTOutput
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.
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>inputIEnumerable<TInput>areRepresentingTheSameItemFunc<TInput, TOutput, bool>fromInputTypeToOutputTypeConversionFunc<TInput, TOutput>addActionAction<TOutput>removeActionAction<TOutput>
Type Parameters
TInputTOutput