Class CollectionUtilities
Provides utility methods for working with collections.
Inheritance
Inherited Members
Namespace: Terrasoft.Common
Assembly: Terrasoft.Common.dll
Syntax
public static class CollectionUtilities
Methods
AddIfNotExists<T>(ICollection<T>, T)
Adds an element to the specified source if it is not part of the source.
Declaration
public static bool AddIfNotExists<T>(this ICollection<T> source, T item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | source | Source. |
T | item | Item. |
Returns
Type | Description |
---|---|
System.Boolean | Flag that indicates when item was added or not. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
AddRange<T>(ICollection<T>, IEnumerable<T>)
Adds a list of elements to the specified source.
Declaration
public static void AddRange<T>(this ICollection<T> source, IEnumerable<T> collection)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | source | Source. |
System.Collections.Generic.IEnumerable<T> | collection | List of elements - class that implements the IEnumerable<T> interface. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the Collection class.
AddRangeIfNotExists<T>(ICollection<T>, IEnumerable<T>)
Adds collection elements to the specified source if they have not been added already.
Declaration
public static void AddRangeIfNotExists<T>(this ICollection<T> source, IEnumerable<T> collection)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | source | Source. |
System.Collections.Generic.IEnumerable<T> | collection | Collection. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the Collection class.
Compare<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Declaration
public static bool Compare<TSource>(this IEnumerable<TSource> source, IEnumerable<TSource> secondSource)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<TSource> | source | |
System.Collections.Generic.IEnumerable<TSource> | secondSource |
Returns
Type | Description |
---|---|
System.Boolean |
Type Parameters
Name | Description |
---|---|
TSource |
CompareZip<TFirst, TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst, TSecond, Boolean>)
Compares two sequences item by item using specified predicate function.
Declaration
public static bool CompareZip<TFirst, TSecond>(this IEnumerable<TFirst> source, IEnumerable<TSecond> secondSource, Func<TFirst, TSecond, bool> comparePredicate)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<TFirst> | source | First sequence. |
System.Collections.Generic.IEnumerable<TSecond> | secondSource | Second sequence. |
System.Func<TFirst, TSecond, System.Boolean> | comparePredicate | Function that compares the corresponding items of the sequences. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Type Parameters
Name | Description |
---|---|
TFirst | Type of elements of the first inbound sequence. |
TSecond | Type of elements of the second inbound sequence. |
Remarks
If the collections have different number of items in them, the number of comparisons will be equal to the number of items in the lesser collection.
Contains(IEnumerable<Guid>, Guid)
Determines whether the instance of GUID belongs to the specified list System.Guid.
Declaration
public static bool Contains(this IEnumerable<Guid> source, Guid item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Guid> | source | The System.Guid list. |
System.Guid | item | The instance of GUID that must be found in System.Guid |
Returns
Type | Description |
---|---|
System.Boolean |
|
Except<T>(IEnumerable<T>, IEnumerable<T>, Func<T, T, Boolean>)
Gets enumerator of items of the source
sequence that are not present in the secondSource
sequence, using the comparer
predicate to determine equality.
Declaration
public static IEnumerable<T> Except<T>(this IEnumerable<T> source, IEnumerable<T> secondSource, Func<T, T, bool> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | First sequence. |
System.Collections.Generic.IEnumerable<T> | secondSource | Second sequence. |
System.Func<T, T, System.Boolean> | comparer | Function that compares the items of the sequences. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The enumerator that contains items from the first sequence that are absent from the second sequence. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the |
Find<T>(IEnumerable<T>, Predicate<T>)
Search an element by specified criteria.
Declaration
public static T Find<T>(this IEnumerable<T> source, Predicate<T> match)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Searched element. |
System.Predicate<T> | match | Search criteria. |
Returns
Type | Description |
---|---|
T | Found objects. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the classes that implement IEnumerable<T> interface.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the |
Find<T>(IEnumerable<T>, Predicate<T>, T)
Search an element by specified criteria.
Declaration
public static T Find<T>(this IEnumerable<T> source, Predicate<T> match, T defValue)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Searched object. |
System.Predicate<T> | match | Search criteria. |
T | defValue | Search result by default. |
Returns
Type | Description |
---|---|
T | Found objects if the search was successful; otherwise - default result ( |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the classes that implement IEnumerable interface.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the |
ForEach<T>(IEnumerable<T>, Action<T>)
Assigns a method to each element of the source.
Declaration
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Source. |
System.Action<T> | action | Assigned method. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the classes that implement IEnumerable interface.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the |
ForEach<T>(IEnumerable<T>, Predicate<T>, Action<T>)
Assigns a method to each element of the source that meets the specified criteria.
Declaration
public static void ForEach<T>(this IEnumerable<T> source, Predicate<T> match, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Source. |
System.Predicate<T> | match | Criteria. |
System.Action<T> | action | Assigned method. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the classes that implement IEnumerable interface.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If one of the parameters passed in ( |
ForEachAsync<T>(IEnumerable<T>, Func<T, Task>)
Invokes asynchronous action for each item in collection.
Declaration
public static Task ForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> action)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Items collection. |
System.Func<T, System.Threading.Tasks.Task> | action | Asynchronous action to invoke on each item. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task that represents asynchronous operation. |
Type Parameters
Name | Description |
---|---|
T | Collection item type. |
GetItemsString<T>(IEnumerable<T>)
Returns a list of objects as a string. Comma is used as delimiter.
Declaration
public static string GetItemsString<T>(this IEnumerable<T> source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | List of objects. |
Returns
Type | Description |
---|---|
System.String | String presentation of objects. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible method for the classes that implement IEnumerable interface.
GetItemsString<T>(IEnumerable<T>, String)
Returns a list of objects as a string. delimiter
is used as delimiter.
Declaration
public static string GetItemsString<T>(this IEnumerable<T> source, string delimiter)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | List of objects. |
System.String | delimiter | Delimiter. |
Returns
Type | Description |
---|---|
System.String | String presentation of objects. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible method for the classes that implement IEnumerable interface.
IsEmpty(IEnumerable)
Determines whether the specified enumerator is empty.
Declaration
public static bool IsEmpty(this IEnumerable source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | source | The enumerator. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsNotEmpty(IEnumerable)
Determines whether the specified enumerator is not empty.
Declaration
public static bool IsNotEmpty(this IEnumerable source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | source | The enumerator. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsNotNullOrEmpty(IEnumerable)
Checks of the specified string is not null
or empty.
Declaration
public static bool IsNotNullOrEmpty(this IEnumerable source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | source | String. |
Returns
Type | Description |
---|---|
System.Boolean |
|
IsNullOrEmpty(IEnumerable)
Determines if the set enumerator is empty or null
.
Declaration
public static bool IsNullOrEmpty(this IEnumerable source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | source | The enumerator. |
Returns
Type | Description |
---|---|
System.Boolean |
|
ParallelForEachAsync<T>(IEnumerable<T>, Func<T, Task>)
Invokes asynchronous action for each item in collection in parallel.
Declaration
public static Task ParallelForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> action)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Items collection. |
System.Func<T, System.Threading.Tasks.Task> | action | Asynchronous action to invoke on each item. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task that represents asynchronous operation. |
Type Parameters
Name | Description |
---|---|
T | Collection item type. |
ParallelForEachAsync<T>(IEnumerable<T>, Func<T, Task>, Int32)
Invokes asynchronous action for each item in collection in parallel.
Declaration
public static Task ParallelForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> action, int maxDegreeOfParallelism)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Items collection. |
System.Func<T, System.Threading.Tasks.Task> | action | Asynchronous action to invoke on each item. |
System.Int32 | maxDegreeOfParallelism | Maximum degree of parallelism. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Task that represents asynchronous operation. |
Type Parameters
Name | Description |
---|---|
T | Collection item type. |
RemoveRange<T>(ICollection<T>, IEnumerable<T>)
Deletes a list of elements from the specified source.
Declaration
public static void RemoveRange<T>(this ICollection<T> source, IEnumerable<T> collection)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<T> | source | Source. |
System.Collections.Generic.IEnumerable<T> | collection | List of elements - class that implements the IEnumerable<T> interface. |
Type Parameters
Name | Description |
---|---|
T | Any type. |
Remarks
Extensible type for the Collection class.
SplitOnChunks<T>(IEnumerable<T>, Int32)
Declaration
public static IEnumerable<IEnumerable<T>> SplitOnChunks<T>(this IEnumerable<T> source, int chunkSize)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | |
System.Int32 | chunkSize |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>> |
Type Parameters
Name | Description |
---|---|
T |
SplitOnParts<T>(IEnumerable<T>, Int32)
Splits collection to the specified parts number.
Declaration
public static IEnumerable<IEnumerable<T>> SplitOnParts<T>(this IEnumerable<T> source, int part)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | Source collection |
System.Int32 | part | Parts count. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>> |
Type Parameters
Name | Description |
---|---|
T |
TryGetItemByIndex<TItem>(Collection<TItem>, Int32, out TItem)
Tries to get an item of the collection by the given index.
Declaration
public static bool TryGetItemByIndex<TItem>(this Collection<TItem> source, int index, out TItem item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.ObjectModel.Collection<TItem> | source | The source collection. |
System.Int32 | index | The index. |
TItem | item | The item. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Type Parameters
Name | Description |
---|---|
TItem | The type of the item. |
Remarks
Used for thread-unsafe collections, when is not important to get item of the collection in actual state.