NamespaceSystemLibrary.Common.Framework.Extensions SystemLibrary.Common.Framework.Net.dll
This class contains extension methods for IEnumerables
IEnumerableExtensions
public static class IEnumerableExtensions
Inheritance
object
IEnumerableExtensions
Examples
Arguments
X
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
X
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
X
Generic types
Name | Description |
---|---|
T | |
TKey |
Methods arguments
Type | Name | Description |
---|---|---|
IEnumerable<T> | items | |
Func<T, TKey> | selector |
X
Type | Description |
---|---|
IEnumerable<T> | IEnumerable filtered on the property of your choice |
Check if the enumerable contains 'value', does not throw if value is null
Remarks
X
Uses Linq.Contains() internally
public static bool Has<T>(this IEnumerable<T> enumerable, object value) where T : class
X
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
X
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | |
object | value |
X
Type | Description |
---|---|
bool | Returns true or false |
Check if the IEnumerable contains 'value'
Does not throw on null
Remarks
X
Uses Linq.Contains() internally, but does not throw on null
public static bool Has<T>(this IEnumerable<T> enumerable, T value) where T : IComparable, IConvertible
X
var texts = new string[] { "Hello", "World" };
var has = texts.Has("Abc");
//has is False
X
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | |
T | value |
X
Type | Description |
---|---|
bool | Returns true or false |
Checks if the Enumerable exists and has at least 1 item
Remarks
X
Does not throw exception on null
public static bool IsNot<T>(this IEnumerable<T> enumerable)
X
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
X
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable |
X
Type | Description |
---|---|
bool | True or false |
Checks if the Enumerable exists and has at least 1 item
Remarks
X
Does not throw exception on null
public static bool Is<T>(this IEnumerable<T> enumerable)
X
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
X
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable |
X
Type | Description |
---|---|
bool | True or false |
}