Class ListUtil
Defines a helper class for work with List.
Inherited Members
Namespace:
Assembly: .dll
Syntax
public class ListUtil
Constructors
ListUtil()
Declaration
public ListUtil()
Methods
<T,TResult>select(List<T> collection, Func1<T,TResult> selector)
Selects all items from the list and transforms them according to provided selector.
Declaration
public static List<TResult> <T,TResult>select(List<T> collection, Func1<T,TResult> selector)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Func1<T,TResult> | selector | The selector which transform list item to target type. |
Returns
| Type | Description |
|---|---|
| List<TResult> | The list with selected items. |
<T,TResult>select(List<TResult> destination, List<T> source, Func1<T,TResult> selector)
Selects all items from the list and transforms them according to provided selector.
Declaration
public static void <T,TResult>select(List<TResult> destination, List<T> source, Func1<T,TResult> selector)
Parameters
| Type | Name | Description |
|---|---|---|
| List<TResult> | destination | The list where selected items should be saved |
| List<T> | source | The list to process. |
| Func1<T,TResult> | selector | The selector which transform list item to target type. |
<T>all(List<T> collection, Predicate<T> predicate)
Checks whether all list items satisfy specified predicate.
Declaration
public static boolean <T>all(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| boolean | True if all list items satisfy specified predicate. |
<T>any(List<T> collection, Predicate<T> predicate)
Checks whether the list contains at least one item which satisfies specified predicate.
Declaration
public static boolean <T>any(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| boolean | True if list contains at least one item which satisfies specified predicate. |
<T>containsExact(List<T> collection, T item)
Checks whether collection contain exactly the same item ( without equals comparison )
Declaration
public static boolean <T>containsExact(List<T> collection, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The collection to check |
| T | item | The item whose presence needs to be tested |
Returns
| Type | Description |
|---|---|
| boolean |
|
<T>count(List<T> collection, Predicate<T> predicate)
Gets count of items from the list which satisfy specified predicate.
Declaration
public static int <T>count(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| int | Count of items from the list which satisfy specified predicate. |
<T>first(List<T> collection)
Selects the first item from the list, if list is empty throws exception.
Declaration
public static T <T>first(List<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
Returns
| Type | Description |
|---|---|
| T | The first item from the list. |
<T>first(List<T> collection, Predicate<T> predicate)
Selects the first item from the list which satisfies specified predicate, if list is empty or match not found throws exception.
Declaration
public static T <T>first(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The first item from the list which satisfies specified predicate. |
<T>firstOrDefault(List<T> collection)
Selects the first item from the list, if list is empty returns null.
Declaration
public static T <T>firstOrDefault(List<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
Returns
| Type | Description |
|---|---|
| T | The first item from the list, or null if list is empty. |
<T>firstOrDefault(List<T> collection, Predicate<T> predicate)
Selects the first item from the list which satisfies specified predicate, if list is empty or match not found returns null.
Declaration
public static T <T>firstOrDefault(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The first item from the list which satisfies specified predicate,, or null if list is empty. |
<T>isNullOrEmpty(List<T> collection)
Checks whether list is null or empty.
Declaration
public static boolean <T>isNullOrEmpty(List<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to check. |
Returns
| Type | Description |
|---|---|
| boolean | True if list is null or empty. |
<T>last(List<T> collection)
Selects the last item from the list, if list is empty throws exception.
Declaration
public static T <T>last(List<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
Returns
| Type | Description |
|---|---|
| T | The last item from the list. |
<T>last(List<T> collection, Predicate<T> predicate)
Selects the last item from the list which satisfies specified predicate, if list is empty or match not found throws exception.
Declaration
public static T <T>last(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The last item from the list which satisfies specified predicate. |
<T>lastOrDefault(List<T> collection)
Selects the latest item from the list, if list is empty returns null.
Declaration
public static T <T>lastOrDefault(List<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
Returns
| Type | Description |
|---|---|
| T | The last item from the list, or null if list is empty. |
<T>lastOrDefault(List<T> collection, Predicate<T> predicate)
Selects the last item from the list which satisfies specified predicate, if list is empty or match not found returns null.
Declaration
public static T <T>lastOrDefault(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The last item from the list which satisfies specified predicate,, or null if list is empty. |
<T>safeAddExact(List<T> collection, T item)
Adds items into list if it wasn't added before, otherwise nothing happens.
Declaration
public static void <T>safeAddExact(List<T> collection, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to add item to. |
| T | item | The item to add into the list. |
<T>single(List<T> collection, Predicate<T> predicate)
Selects single item which satisfies specified predicate, if there are more than one item which satisfies predicate then throws exception.
Declaration
public static T <T>single(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The single item. |
<T>singleOrDefault(List<T> collection, Predicate<T> predicate)
Selects single item which satisfies specified predicate, If list is empty returns null and if there are more than one item which satisfies predicate then throws exception.
Declaration
public static T <T>singleOrDefault(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| T | The single item. |
<T>where(List<T> collection, Predicate<T> predicate)
Selects all items from the list which satisfy specified predicate.
Declaration
public static List<T> <T>where(List<T> collection, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | collection | The list to process. |
| Predicate<T> | predicate | The predicate to check. |
Returns
| Type | Description |
|---|---|
| List<T> | The list with selected items. |
<T>where(List<T> destination, List<T> source, Predicate<T> predicate)
Selects all items from the list which satisfy specified predicate.
Declaration
public static void <T>where(List<T> destination, List<T> source, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| List<T> | destination | The list where selected items should be saved |
| List<T> | source | The list to process. |
| Predicate<T> | predicate | The predicate to check. |