Class IEnumerableExtensions
IEnumerable 扩展方法
Inherited Members
Namespace: Lett.Extensions
Assembly: Lett.Extensions.dll
Syntax
public static class IEnumerableExtensions
Methods
| Improve this Doc View SourceContainsAll<T>(IEnumerable<T>, IEnumerable<T>)
是否包含全部元素
this
是否包含 items
中全部元素
Declaration
public static bool ContainsAll<T>(this IEnumerable<T> this, IEnumerable<T> items)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
IEnumerable<T> | items | 需要进行判断的元素集合 |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T | 集合中元素的类型 |
Examples
var arr = new[] {"aa", "bb"};
var match = new[] {"aa"};
var match2 = new[] {"aa", "bb"};
var match2 = new[] {"aa", "bb", "cc"};
var rs = arr.ContainsAll(match); // true;
var rs2 = arr.ContainsAll(match2); // true;
var rs3 = arr.ContainsAll(match3); // false;
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ContainsAny<T>(IEnumerable<T>, IEnumerable<T>)
是否包含任一元素
this
是否包含 items
中任一元素
Declaration
public static bool ContainsAny<T>(this IEnumerable<T> this, IEnumerable<T> items)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
IEnumerable<T> | items | 需要进行判断的元素集合 |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T | 集合中元素的类型 |
Examples
var arr = new[] {"aa", "bb"};
var match = new[] {"aa", "bb"};
var match2 = new [] {"aa", "bb", "cc"};
var rs = arr.ContainsAny(match); // true
var rs2 = arr.ContainsAny(match2); // true
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
Distinct<T, TResult>(IEnumerable<T>, Func<T, TResult>)
返回序列中的非重复元素
Declaration
public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> this, Func<T, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Func<T, TResult> | selector | 选择器,选择用于比较的对象 |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T | |
TResult |
Examples
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs1 = input.Distinct(s => s.Name).ToList();
rs1.Count; // 3
rs1[0].Name; // "a"
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs2 = input.Distinct(s => s.Age).ToList();
rs2.Count; // 1
rs2[0]; // "a"
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
Distinct<T, TResult>(IEnumerable<T>, Func<T, TResult>, IEqualityComparer<TResult>)
返回序列中的非重复元素
Declaration
public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> this, Func<T, TResult> selector, IEqualityComparer<TResult> equalityComparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Func<T, TResult> | selector | 选择器,选择用于比较的对象 |
IEqualityComparer<TResult> | equalityComparer | 比较器 |
Returns
Type | Description |
---|---|
IEnumerable<T> |
Type Parameters
Name | Description |
---|---|
T | |
TResult |
Examples
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs1 = input.Distinct(s => s.Name, StringComparer.OrdinalIgnoreCase)
.ToList();
rs1.Count; // 2
rs1[0].Name; // "a"
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs2 = input.Distinct(s => s.Age, EqualityComparer<int>.Default)
.ToList();
rs2.Count; // 1
rs2[0]; // "a"
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ArgumentNullException |
|
Duplicates<T, TResult>(IEnumerable<T>, Func<T, TResult>)
返回序列中重复的元素
Declaration
public static IEnumerable<TResult> Duplicates<T, TResult>(this IEnumerable<T> this, Func<T, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Func<T, TResult> | selector | 选择器,选择用于比较的对象 |
Returns
Type | Description |
---|---|
IEnumerable<TResult> |
Type Parameters
Name | Description |
---|---|
T | |
TResult |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
Duplicates<T, TResult>(IEnumerable<T>, Func<T, TResult>, IEqualityComparer<TResult>)
返回序列中重复的元素
Declaration
public static IEnumerable<TResult> Duplicates<T, TResult>(this IEnumerable<T> this, Func<T, TResult> selector, IEqualityComparer<TResult> equalityComparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Func<T, TResult> | selector | 选择器,选择用于比较的对象 |
IEqualityComparer<TResult> | equalityComparer | 比较器 |
Returns
Type | Description |
---|---|
IEnumerable<TResult> |
Type Parameters
Name | Description |
---|---|
T | |
TResult |
Examples
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs1 = input.Duplicates(s => s.Name, StringComparer.OrdinalIgnoreCase);
// rs1 = {"a"}
var input = new List<MyClass> {new MyClass {Age = 2, Name = "a"}, new MyClass {Age = 2, Name = "A"}, new MyClass {Age = 2, Name = "b"}};
var rs1 = input.Duplicates(s => s.Name);
// rs1 = {}
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ArgumentNullException |
|
ForEach<T>(IEnumerable<T>, Action<T>)
对指定集合的每个元素执行指定操作。
Declaration
public static void ForEach<T>(this IEnumerable<T> this, Action<T> action)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Action<T> | action | 指定操作 T: 元素 |
Type Parameters
Name | Description |
---|---|
T | 元素类型 |
Examples
var arr = new[] {"aa", "bb"};
var rs = new List<string>();
arr.ForEach(str => rs.Add(str));
// rs[0] = "aa"
// rs[1] = "bb"
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
ForEach<T>(IEnumerable<T>, Action<Int32, T>)
对指定集合的每个元素执行指定操作。
Declaration
public static void ForEach<T>(this IEnumerable<T> this, Action<int, T> action)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Action<Int32, T> | action | 指定操作 int: 数组元素的索引 T: 元素 |
Type Parameters
Name | Description |
---|---|
T | 元素类型 |
Examples
var arr = new[] {"aa", "bb"};
var rs = new Dictionary<int, string>();
arr.ForEach((index, str) => rs.Add(index, str));
// rs[0] = "aa"
// rs[1] = "bb"
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|
IsNullOrEmpty<T>(IEnumerable<T>)
是否空或空集合
Declaration
public static bool IsNullOrEmpty<T>(this IEnumerable<T> this)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this |
Returns
Type | Description |
---|---|
Boolean |
Type Parameters
Name | Description |
---|---|
T | 集合中元素的类型 |
Examples
var arr = new[] {"aa", "bb"};
var rs = arr.IsNullOrEmpty(); // false
var arr2 = new string[] { };
var rs2 = arr.IsNullOrEmpty(); // true
|
Improve this Doc
View Source
SplitBlock<T>(IEnumerable<T>, Int32)
分割成指定size的块
new[] {1, 2, 3, 4, 5, 6, 7}.SplitBlock(3); -> {{1, 2, 3}, {4, 5, 6}, {7}}
Declaration
public static IEnumerable<IEnumerable<T>> SplitBlock<T>(this IEnumerable<T> this, int size)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
Int32 | size | size |
Returns
Type | Description |
---|---|
IEnumerable<IEnumerable<T>> |
Type Parameters
Name | Description |
---|---|
T |
Examples
var s = new[] {1, 2, 3, 4, 5, 6, 7};
var rs = s.Block(2); // rs = {{1, 2}, {3, 4}, {5, 6}, {7}}
var rs2 = s.Block(3); // rs2 = {{1, 2, 3}, {4, 5, 6}, {7}}
var rs3 = s.Block(4); // rs3 = {{1, 2, 3, 4}, {5, 6, 7}}
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentException |
|
ToFormatString<T>(IEnumerable<T>, String, Func<T, Object[]>)
格式化为字符串
Declaration
public static string ToFormatString<T>(this IEnumerable<T> this, string formatStr, Func<T, object[]> formatParams)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | this | |
String | formatStr | 格式化字符串,如:"[{0}]" |
Func<T, Object[]> | formatParams | 格式化字符串参数,需要与格式化字符串对应 |
Returns
Type | Description |
---|---|
String |
Type Parameters
Name | Description |
---|---|
T |
Examples
var input = new List<string> {"aa", "bb", "cc", "dd"};
var rs = input.FormatString("[{0}]", s => new []{s});
// rs = "[aa][bb][cc][dd]"
var input2 = new Dictionary<string,object>
{
{"key1",1},
{"key2","value2"},
{"key3","value3"}
};
var rs2 = input2.FormatString("[{0} - {1}]\r\n", pair => new[] {pair.Key, pair.Value});
// rs2 = "[key1 - 1]\r\n[key2 - value2]\r\n[key3 - value3]\r\n"
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FormatException |