Class ListExtensions
Provides extension methods for working with lists and collections.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public static class ListExtensions
Methods
AddRange<T>(List<T>, params T[])
Adds a range of items to the end of the list.
Declaration
public static void AddRange<T>(this List<T> list, params T[] items)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | list | The list to which items are added. |
| T[] | items | The items to be added. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the list. |
Batch<T>(IEnumerable<T>, int)
Batches the elements of a sequence into smaller sequences of a specified maximum size.
Declaration
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int maxItems)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items | The sequence to be batched. |
| int | maxItems | The maximum number of items in each batch. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IEnumerable<T>> | An IEnumerable of batches containing items from the input sequence. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the sequence. |
Batch<T>(IQueryable<T>, int)
Batches the elements of an IQueryable collection into smaller sequences of a specified size.
Declaration
public static IEnumerable<IQueryable<T>> Batch<T>(this IQueryable<T> collection, int size)
Parameters
| Type | Name | Description |
|---|---|---|
| IQueryable<T> | collection | The IQueryable collection to be batched. |
| int | size | The size of each batch. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IQueryable<T>> | An IEnumerable of IQueryable batches from the input collection. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the collection. |
Combinations<T>(List<T>)
Generates all combinations of the elements in the list.
Declaration
public static List<List<T>> Combinations<T>(this List<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | list | The list for which combinations are generated. |
Returns
| Type | Description |
|---|---|
| List<List<T>> | A List of Lists representing all combinations of the list's elements. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the list. |
Denullify<T>(IEnumerable<T>)
Removes null elements from the IEnumerable.
Declaration
public static IEnumerable<T> Denullify<T>(this IEnumerable<T> items)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | items | The IEnumerable containing elements to be filtered. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An IEnumerable with null elements removed. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the IEnumerable. |
Median<T>(IEnumerable<T>)
Computes the median of a sequence of numbers.
Declaration
public static T Median<T>(this IEnumerable<T> source) where T : struct, IComparable
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The sequence of numbers. |
Returns
| Type | Description |
|---|---|
| T | The median value, returned as the same type as the input. |
Type Parameters
| Name | Description |
|---|---|
| T | The numeric type of the elements in the sequence. Supported types are decimal, double, int, etc. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if the source sequence is null or empty. |
Mode<T>(IEnumerable<T>)
Computes the mode(s) of a sequence of elements, i.e., the element(s) that appear most frequently.
Declaration
public static IEnumerable<T> Mode<T>(this IEnumerable<T> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | source | The sequence of elements. |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An IEnumerable containing the mode(s) of the sequence. If there is more than one mode, all modes are returned; if no mode exists (all elements are unique), all elements are returned. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the elements in the sequence. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if the source sequence is null or empty. |
Permutations<T>(List<T>)
Generates all permutations of the elements in the list.
Declaration
public static List<List<T>> Permutations<T>(this List<T> list)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | list | The list for which permutations are generated. |
Returns
| Type | Description |
|---|---|
| List<List<T>> | A List of Lists representing all permutations of the list's elements. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the list. |
Permutations<T>(List<T>, int)
Generates permutations of a specified length from the elements in the list.
Declaration
public static List<List<T>> Permutations<T>(this List<T> list, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | list | The list for which permutations are generated. |
| int | length | The length of permutations to generate. |
Returns
| Type | Description |
|---|---|
| List<List<T>> | A List of Lists representing permutations of the specified length. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of elements in the list. |