Class EnumerableExtensions
Extensions for enumerable sources
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class EnumerableExtensions
Methods
View SourceContainsAll<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Returns true if all items in the other collection exist in this collection
Declaration
public static bool ContainsAll<TSource>(this IEnumerable<TSource> source, IEnumerable<TSource> other)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | |
| IEnumerable<TSource> | other |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TSource |
ContainsAny<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Returns true if the source contains any of the items in the other list
Declaration
public static bool ContainsAny<TSource>(this IEnumerable<TSource> source, IEnumerable<TSource> other)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | |
| IEnumerable<TSource> | other |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| TSource |
EmptyNull<T>(IEnumerable<T>?)
Gets a null IEnumerable as an empty IEnumerable.
Declaration
public static IEnumerable<T> EmptyNull<T>(this IEnumerable<T>? items)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
FindIndex<T>(IEnumerable<T>, Func<T, bool>)
Finds the index of the first item matching an expression in an enumerable.
Declaration
public static int FindIndex<T>(this IEnumerable<T> items, Func<T, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items | The enumerable to search. |
| Func<T, bool> | predicate | The expression to test the items against. |
Returns
| Type | Description |
|---|---|
| int | The index of the first matching item, or -1. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the enumerated objects. |
FindIndex<T>(IEnumerable<T>, int, Func<T, bool>)
Finds the index of the first item matching an expression in an enumerable.
Declaration
public static int FindIndex<T>(this IEnumerable<T> items, int startIndex, Func<T, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items | The enumerable to search. |
| int | startIndex | The index to start at. |
| Func<T, bool> | predicate | The expression to test the items against. |
Returns
| Type | Description |
|---|---|
| int | The index of the first matching item, or -1. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the enumerated objects. |
ForAllThatAre<TBase, TActual>(IEnumerable<TBase>, Action<TActual>)
Applies a projection action to all items in the sequence that are of the specified actual type.
Declaration
public static IEnumerable<TBase> ForAllThatAre<TBase, TActual>(this IEnumerable<TBase> sequence, Action<TActual> projection) where TActual : class
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TBase> | sequence | The source sequence. |
| Action<TActual> | projection | The action to apply to each item of the actual type. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TBase> | The original sequence, allowing chaining. |
Type Parameters
| Name | Description |
|---|---|
| TBase | The base type of items in the sequence. |
| TActual | The actual type to filter and apply the projection to. |
IfNotNull<TItem>(IEnumerable<TItem>, Action<TItem>)
The if not null.
Declaration
public static void IfNotNull<TItem>(this IEnumerable<TItem> items, Action<TItem> action) where TItem : class
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TItem> | items | The items. |
| Action<TItem> | action | The action. |
Type Parameters
| Name | Description |
|---|---|
| TItem | The type |
InGroupsOf<T>(IEnumerable<T>?, int)
Splits the enumerable into groups of the specified size.
Declaration
public static IEnumerable<IEnumerable<T>> InGroupsOf<T>(this IEnumerable<T>? source, int groupSize)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source enumerable. |
| int | groupSize | The size of each group. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IEnumerable<T>> | An enumerable of groups, each containing up to the specified number of items. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the enumerable. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when source is null. |
| ArgumentException | Thrown when groupSize is less than or equal to zero. |
IndexOf<T>(IEnumerable<T>, T)
Finds the index of the first occurrence of an item in an enumerable.
Declaration
public static int IndexOf<T>(this IEnumerable<T> items, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items | The enumerable to search. |
| T | item | The item to find. |
Returns
| Type | Description |
|---|---|
| int | The index of the first matching item, or -1 if the item was not found. |
Type Parameters
| Name | Description |
|---|---|
| T |
IsCollectionEmpty<T>(IReadOnlyCollection<T>?)
Determines whether the collection is null or empty.
Declaration
public static bool IsCollectionEmpty<T>(this IReadOnlyCollection<T>? list)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyCollection<T> | list | The collection to check. |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the collection. |
OfTypes<T>(IEnumerable<T>, params Type[])
Filters a sequence to include only items whose type is in the specified list of types.
Declaration
public static IEnumerable<T> OfTypes<T>(this IEnumerable<T> contents, params Type[] types)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | contents | The source sequence. |
| Type[] | types | The types to include. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | A filtered sequence containing only items of the specified types. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the sequence. |
Remarks
This is similar to .OfType<T>() but supports filtering with multiple types.
OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, Direction)
Orders the elements of a sequence according to a key and the specified sort direction.
Declaration
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Direction sortOrder)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence of values to order. |
| Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
| Direction | sortOrder | The direction to sort (ascending or descending). |
Returns
| Type | Description |
|---|---|
| IOrderedEnumerable<TSource> | An ordered enumerable. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of source. |
| TKey | The type of the key returned by keySelector. |
Range<T>(Func<int, T>, int)
Returns a sequence of length count whose elements are the result of invoking
factory.
Declaration
public static IEnumerable<T> Range<T>(Func<int, T> factory, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<int, T> | factory | The factory. |
| int | count | The count. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
RemoveAll<T>(ICollection<T>, Func<T, bool>)
Removes all matching items from an System.Collections.Generic.ICollection<T>.
Declaration
public static void RemoveAll<T>(this ICollection<T> list, Func<T, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<T> | list | The list. |
| Func<T, bool> | predicate | The predicate. |
Type Parameters
| Name | Description |
|---|---|
| T |
RemoveAll<T>(IList<T>, Func<T, bool>)
Removes all matching items from an System.Collections.Generic.IList<T>.
Declaration
public static void RemoveAll<T>(this IList<T> list, Func<T, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IList<T> | list | The list. |
| Func<T, bool> | predicate | The predicate. |
Type Parameters
| Name | Description |
|---|---|
| T |
SelectByGroups<TResult, TSource>(IEnumerable<TSource>, Func<IEnumerable<TSource>, IEnumerable<TResult>>, int)
Selects items by processing the source in groups of the specified size.
Declaration
public static IEnumerable<TResult> SelectByGroups<TResult, TSource>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> selector, int groupSize)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source enumerable. |
| Func<IEnumerable<TSource>, IEnumerable<TResult>> | selector | The selector function to apply to each group. |
| int | groupSize | The size of each group. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TResult> | An enumerable of results from processing each group. |
Type Parameters
| Name | Description |
|---|---|
| TResult | The type of the result items. |
| TSource | The type of the source items. |
SelectRecursive<TSource>(IEnumerable<TSource>, Func<TSource, IEnumerable<TSource>>, int)
Recursively selects items from the source using the specified selector.
Declaration
public static IEnumerable<TSource> SelectRecursive<TSource>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> recursiveSelector, int maxRecusionDepth = 100)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The source enumerable. |
| Func<TSource, IEnumerable<TSource>> | recursiveSelector | A function that returns child elements for each item. |
| int | maxRecusionDepth | The maximum recursion depth to prevent stack overflow. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TSource> | A flattened enumerable of all items and their recursive children. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the source items. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the maximum recursion depth is exceeded. |
SkipLast<T>(IEnumerable<T>)
Returns all elements of the sequence except the last one.
Declaration
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The source enumerable. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An enumerable containing all elements except the last. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of items in the enumerable. |
Transform<TSource, TTarget>(IEnumerable<TSource>, Func<IEnumerable<TSource>, IEnumerable<TTarget>>)
Transforms an enumerable.
Declaration
public static IEnumerable<TTarget> Transform<TSource, TTarget>(this IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TTarget>> transform)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | |
| Func<IEnumerable<TSource>, IEnumerable<TTarget>> | transform |
Returns
| Type | Description |
|---|---|
| IEnumerable<TTarget> |
Type Parameters
| Name | Description |
|---|---|
| TSource | |
| TTarget |
UnsortedSequenceEqual<T>(IEnumerable<T>?, IEnumerable<T>?)
Determines if 2 lists have equal elements within them regardless of how they are sorted
Declaration
public static bool UnsortedSequenceEqual<T>(this IEnumerable<T>? source, IEnumerable<T>? other)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | |
| IEnumerable<T> | other |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The logic for this is taken from: http://stackoverflow.com/questions/4576723/test-whether-two-ienumerablet-have-the-same-values-with-the-same-frequencies There's a few answers, this one seems the best for it's simplicity and based on the comment of Eamon
WhereNotNull<T>(IEnumerable<T?>)
Filters a sequence of values to ignore those which are null.
Declaration
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> coll) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | coll | The coll. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
Yield<T>(T)
Wraps this object instance into an IEnumerable{T} consisting of a single item.
Declaration
public static IEnumerable<T> Yield<T>(this T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The instance that will be wrapped. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An IEnumerable{T} consisting of a single item. |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the object. |