NamespaceSystemLibrary.Common.Net.Extensions SystemLibrary.Common.Net.dll
This class contains extension methods for IEnumerables
For instance: Has, DistinctBy, Is, ...
IEnumerableExtensions
public static class IEnumerableExtensions
Inheritance
Examples
Arguments
Methods
Select items grouped by a property of your choice
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> selector) where T : class
class Car
{
public string Name;
}
var car1 = new Car { Name = "Vehicle" }
var car2 = new Car { Name = "Vehicle" }
var list = new List<Car> { car1, car2 }
var list = list.DistinctBy(x => x.Name).ToList();
// list contains now 1 car
Generic types
Name | Description |
---|---|
T | |
TKey |
Methods arguments
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | items | |
System.Func<T, TKey> | selector |
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | A new list filtered on the property of your choice |
Check if the enumerable contains 'value'
Does not throw exception if enumerable is null nor if argument 'value' is null
Uses Linq.Contains() internally
public static bool Has<T>(this IEnumerable<T> enumerable, T value) where T : IComparable, IConvertible
var texts = new string[] { "Hello", "World" };
var has = texts.Has("Abc");
//has is False
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable | |
T | value |
Type | Description |
---|---|
System.Boolean | Returns true or false |
Check if the enumerable contains 'value'
Does not throw exception if enumerable is null nor if argument 'value' is null
Uses Linq.Contains() internally
public static bool Has<T>(this IEnumerable<T> enumerable, object value) where T : class
var users = new List<User>();
var user = new User();
users.Add(user);
var result = users.Has(user);
// result is true, the list contains that specific user object
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable | |
System.Object | value |
Type | Description |
---|---|
System.Boolean | Returns true or false |
Check if the enumerable exists, if so it returns true, else if it is null or it has 0 items in it, this returns false Note: the Linq.Any() throws exception if null while this function does not throw exception
public static bool Is<T>(this IEnumerable<T> enumerable)
var list = new List<string>();
list.Add("hello world");
var result = list.Is();
// result is true as the list contains 1 item
List<string> list = null;
var result = list.Is();
// result is false as the list is null
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable |
Type | Description |
---|---|
System.Boolean |
Check if the enumerable does not exist, if so it returns true, else if it is not null and it has at least 1 item, this will return false Note: the Linq.Any() throws exception if null while this function does not throw exception
public static bool IsNot<T>(this IEnumerable<T> enumerable)
var list = new List<string>();
var result = list.IsNot();
// result is true as the list contains 0 items
List<string> list = null;
var result = list.IsNot();
// result is true as the list currently is null
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable |
Type | Description |
---|---|
System.Boolean |
}