跳转到内容

EnumerableExtensions

EnumerableExtensions 是一个静态扩展类,为 IEnumerable<T> 和 ICollection<T> 类型提供了丰富的扩展方法,包括空值判断、安全操作、集合操作、分页排序、统计计算、集合转换、批量操作等功能,旨在简化集合处理,提高代码的安全性和可读性,特别是用于处理可能为空的集合类型的日常操作场景。

提供对可枚举集合的各种安全操作方法。

  • IsNullOrEmpty<T>() - 判断集合是否为 null 或空
  • IsNotEmpty<T>() - 判断集合是否不为空
  • SafeCount<T>() - 获取集合元素数量(安全)
  • CountIf<T>(Func<T, bool> predicate) - 获取集合元素数量(使用条件过滤)
  • FirstOrDefaultSafe<T>() - 获取集合的第一个元素,若为空则返回默认值
  • FirstOrDefaultSafe<T>(Func<T, bool> predicate) - 获取集合的第一个满足条件的元素
  • LastOrDefaultSafe<T>() - 获取集合的最后一个元素,若为空则返回默认值
  • LastOrDefaultSafe<T>(Func<T, bool> predicate) - 获取集合的最后一个满足条件的元素
  • ElementAtOrDefaultSafe<T>(int index) - 获取集合中指定索引的元素,若越界则返回默认值
  • SingleOrDefaultSafe<T>() - 获取集合的单个元素,若为空或包含多个元素则返回默认值
  • SingleOrDefaultSafe<T>(Func<T, bool> predicate) - 获取集合的单个满足条件的元素
  • ContainsSafe<T>(T item) - 判断集合是否包含指定元素(支持 null 集合)
  • ContainsWhere<T>(Func<T, bool> predicate) - 判断集合是否包含满足指定条件的元素
  • ToListSafe<T>() - 将集合转换为 List(安全)
  • ToArraySafe<T>() - 将集合转换为数组(安全)
  • DistinctSafe<T>() - 集合去重(安全)
  • DistinctBySafe<TSource, TKey>(Func<TSource, TKey> keySelector) - 根据指定键去重(安全)
  • WhereSafe<T>(Func<T, bool> predicate) - 集合过滤(安全)
  • SelectSafe<TSource, TResult>(Func<TSource, TResult> selector) - 集合投影(安全)
  • GroupBySafe<TSource, TKey>(Func<TSource, TKey> keySelector) - 集合分组(安全)
  • OrderBySafe<TSource, TKey>(Func<TSource, TKey> keySelector) - 集合排序(安全)
  • OrderByDescendingSafe<TSource, TKey>(Func<TSource, TKey> keySelector) - 集合逆序排序(安全)
  • ReverseSafe<T>() - 集合反转(安全)
  • Shuffle<T>() - 集合随机排序(洗牌)
  • AllSafe<T>(Func<T, bool> predicate) - 判断是否全部满足条件(安全)
  • AnySafe<T>(Func<T, bool> predicate) - 判断是否存在满足条件的元素(安全)
  • SequenceEqualSafe<T>(IEnumerable<T> second) - 判断两个集合是否相等(安全)
  • ContainsSameElements<T>(IEnumerable<T> second) - 判断两个集合是否包含相同元素(忽略顺序)
  • ContainsNull<T>() - 判断集合是否包含 null 元素
  • ConcatSafe<T>(IEnumerable<T> second) - 合并两个集合(安全)
  • ConcatMany<T>(params IEnumerable<T>[] sources) - 合并多个集合
  • IntersectSafe<T>(IEnumerable<T> second) - 获取两个集合的交集(安全)
  • UnionSafe<T>(IEnumerable<T> second) - 获取两个集合的并集(安全)
  • ExceptSafe<T>(IEnumerable<T> second) - 获取两个集合的差集(安全)
  • Page<T>(int pageIndex, int pageSize) - 集合分页(安全)
  • SumSafe() - 集合求和(安全)- 支持 int、double、decimal、float
  • AverageSafe() - 集合平均值(安全)- 支持 int、double、decimal、float
  • MaxSafe() - 集合最大值(安全)- 支持 int、double、decimal、float
  • MinSafe() - 集合最小值(安全)- 支持 int、double、decimal、float
  • MaxBy<TSource, TKey>(Func<TSource, TKey> keySelector) - 获取集合中最大值对应的元素
  • MinBy<TSource, TKey>(Func<TSource, TKey> keySelector) - 获取集合中最小值对应的元素
  • ForEachSafe<T>(Action<T> action) - 集合遍历执行操作(安全)
  • ForEachIndexed<T>(Action<T, int> action) - 集合遍历执行操作(带索引)
  • ForEachAsync<T>(Func<T, Task> action) - 集合遍历执行异步操作
  • ChunkSafe<T>(int size) - 集合分块(按指定大小分组)
  • Batch<T>(int batchSize, Action<IEnumerable<T>> action) - 集合分批处理
  • BatchAsync<T>(int batchSize, Func<IEnumerable<T>, Task> action) - 集合分批异步处理
  • ToDictionarySafe<TSource, TKey, TValue>(Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector) - 集合转换为字典(安全)
  • ToHashSetSafe<T>() - 集合转换为 HashSet(安全)
  • ToDataTable<T>() - 集合转换为 DataTable(自动推断列名和类型)
  • ToConcurrentDictionary<TKey, TValue>() - 集合转换为 ConcurrentDictionary
  • RemoveNulls<T>() - 集合去除 null 元素
  • JoinToString<T>(string separator) - 将集合元素连接为字符串
  • JoinToString<T>(Func<T, string> selector, string separator) - 将集合元素连接为字符串(带选择器)

提供对集合类型的安全操作方法。

  • IsNullOrEmptySafe<T>() - 判断集合是否为 null 或空(IEnumerable)
  • IsNullOrEmptySafe<T>() - 判断集合是否为 null 或空(ICollection)
  • IsNotEmpty<T>() - 判断集合是否不为空
  • AddSafe<T>(T item) - 安全添加元素到集合
  • RemoveSafe<T>(T item) - 安全移除元素
  • ClearSafe<T>() - 清空集合(安全)
  • AddRangeSafe<T>(IEnumerable<T> items) - 批量添加元素(安全)
  • RemoveRangeSafe<T>(IEnumerable<T> items) - 批量移除元素(安全)

判断集合是否为 null 或空。

public static bool IsNullOrEmptySafe<T>(this IEnumerable<T> source)
public static bool IsNullOrEmptySafe<T>(this ICollection<T> collection)

参数:

  • source: 待判断的集合

返回值:

  • 如果集合为 null 或不包含任何元素,返回 true;否则返回 false

示例:

List<int> list1 = null;
List<int> list2 = new List<int>();
List<int> list3 = new List<int> { 1, 2, 3 };
bool result1 = list1.IsNullOrEmptySafe(); // true
bool result2 = list2.IsNullOrEmptySafe(); // true
bool result3 = list3.IsNullOrEmptySafe(); // false

判断集合是否不为空。

public static bool IsNotEmpty<T>(this IEnumerable<T> source)

返回值:

  • 如果集合不为 null 且包含元素,返回 true;否则返回 false

示例:

List<int> list = new List<int> { 1, 2, 3 };
bool result = list.IsNotEmpty(); // true

获取集合元素数量(安全,支持 null 集合)。

public static int SafeCount<T>(this IEnumerable<T> source)

获取满足条件的元素数量。

public static int CountIf<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 条件判断函数

返回值:

  • 满足条件的元素数量

示例:

var list = new List<int> { 1, 2, 3, 4, 5 };
var count = list.CountIf(x => x > 3); // 2

参数:

  • source: 集合

返回值:

  • 集合元素数量,如果集合为 null 则返回 0

示例:

List<int> list1 = null;
List<int> list2 = new List<int> { 1, 2, 3 };
int count1 = list1.SafeCount(); // 0
int count2 = list2.SafeCount(); // 3

获取集合的第一个元素,若为空则返回默认值。

public static T FirstOrDefaultSafe<T>(this IEnumerable<T> source)
public static T FirstOrDefaultSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 过滤条件(可选)

返回值:

  • 第一个元素或默认值

示例:

List<int> list1 = null;
List<int> list2 = new List<int> { 1, 2, 3, 4, 5 };
int result1 = list1.FirstOrDefaultSafe(); // 0 (default)
int result2 = list2.FirstOrDefaultSafe(); // 1
int result3 = list2.FirstOrDefaultSafe(x => x > 3); // 4

获取集合中指定索引的元素,若索引越界则返回默认值。

public static T ElementAtOrDefaultSafe<T>(this IEnumerable<T> source, int index)

参数:

  • source: 集合
  • index: 索引位置

返回值:

  • 指定索引的元素或默认值

示例:

List<int> list = new List<int> { 1, 2, 3 };
int result1 = list.ElementAtOrDefaultSafe(1); // 2
int result2 = list.ElementAtOrDefaultSafe(10); // 0 (default)

获取集合最后一个元素,若集合为空则返回默认值。

public static T LastOrDefaultSafe<T>(this IEnumerable<T> source)
public static T LastOrDefaultSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 过滤条件(可选)

返回值:

  • 最后一个元素或默认值

示例:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int result = list.LastOrDefaultSafe(); // 5
int result2 = list.LastOrDefaultSafe(x => x < 3); // 2

获取集合唯一元素,若集合为空或包含多个元素则返回默认值。

public static T SingleOrDefaultSafe<T>(this IEnumerable<T> source)
public static T SingleOrDefaultSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 过滤条件(可选)

返回值:

  • 唯一元素或默认值

示例:

List<int> list = new List<int> { 5 };
int result = list.SingleOrDefaultSafe(); // 5

判断集合是否包含指定元素(支持 null 集合)。

public static bool ContainsSafe<T>(this IEnumerable<T> source, T item)

参数:

  • source: 集合
  • item: 要查找的元素

返回值:

  • 如果集合包含指定元素返回 true;否则返回 false

示例:

List<int> list1 = null;
List<int> list2 = new List<int> { 1, 2, 3 };
bool result1 = list1.ContainsSafe(2); // false
bool result2 = list2.ContainsSafe(2); // true

根据指定键去重(安全)。

public static IEnumerable<TSource> DistinctBySafe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

参数:

  • source: 集合
  • keySelector: 键选择器

返回值:

  • 去重后的集合

示例:

var list = new List<Person>
{
new Person { Id = 1, Name = "Alice" },
new Person { Id = 2, Name = "Bob" },
new Person { Id = 1, Name = "Alice2" }
};
var distinct = list.DistinctBySafe(x => x.Id).ToList();
// 结果:Id=1 (Alice), Id=2 (Bob)

判断集合是否包含满足指定条件的元素(支持 null 集合)。

public static bool ContainsWhere<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 条件谓词

返回值:

  • 如果集合包含满足条件的元素返回 true;否则返回 false

示例:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
bool result = list.ContainsWhere(x => x > 3); // true

安全地将集合转换为 List(支持 null 集合)。

public static List<T> ToListSafe<T>(this IEnumerable<T> source)

返回值: List 实例,如果源为 null 则返回空 List

安全地将集合转换为数组(支持 null 集合)。

public static T[] ToArraySafe<T>(this IEnumerable<T> source)

返回值: 数组,如果源为 null 则返回空数组

安全地将集合转换为 HashSet(支持 null 集合)。

public static HashSet<T> ToHashSetSafe<T>(this IEnumerable<T> source)

返回值: HashSet 实例,如果源为 null 则返回空 HashSet

安全地将集合转换为字典(支持 null 集合和选择器)。

public static Dictionary<TKey, TValue> ToDictionarySafe<TSource, TKey, TValue>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)

参数:

  • source: 源集合
  • keySelector: 键选择器
  • valueSelector: 值选择器

返回值: 字典实例,如果源为 null 或选择器为 null 则返回空字典

示例:

var users = new[] { new { Id = 1, Name = "Alice" }, new { Id = 2, Name = "Bob" } };
var dict = users.ToDictionarySafe(u => u.Id, u => u.Name);
// dict: { [1] = "Alice", [2] = "Bob" }

将集合转换为 DataTable(自动推断列名和类型)。

public static System.Data.DataTable ToDataTable<T>(this IEnumerable<T> source)

参数:

  • source: 源集合

返回值: DataTable 实例,列名和类型根据 T 的属性自动推断

示例:

var users = new[] { new { Id = 1, Name = "Alice" }, new { Id = 2, Name = "Bob" } };
var table = users.ToDataTable();
// table 包含 Id (Int32) 和 Name (String) 两列

将集合转换为 ConcurrentDictionary(线程安全字典)。

public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source)
public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)

参数:

  • source: 源集合
  • keySelector: 键选择器(第二个重载)
  • valueSelector: 值选择器(第二个重载)

返回值: ConcurrentDictionary 实例,如果源为 null 则返回空 ConcurrentDictionary

示例:

var pairs = new[] { KeyValuePair.Create("a", 1), KeyValuePair.Create("b", 2) };
var dict = pairs.ToConcurrentDictionary();
var users = new[] { new { Id = 1, Name = "Alice" } };
var userDict = users.ToConcurrentDictionary(u => u.Id, u => u.Name);

安全地对集合进行分组(支持 null 集合和选择器)。

public static IEnumerable<IGrouping<TKey, TSource>> GroupBySafe<TSource, TKey>(
this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

参数:

  • source: 源集合
  • keySelector: 分组键选择器

返回值: 分组后的集合,如果源为 null 或选择器为 null 则返回空集合

示例:

var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var groups = numbers.GroupBySafe(n => n % 2 == 0 ? "偶数" : "奇数");
// groups: { "奇数": [1, 3, 5], "偶数": [2, 4, 6] }

安全去重(支持 null 集合)。

public static IEnumerable<T> DistinctSafe<T>(this IEnumerable<T> source)

返回值: 去重后的集合

安全筛选(支持 null 集合)。

public static IEnumerable<T> WhereSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

返回值: 筛选后的集合

安全投影(支持 null 集合)。

public static IEnumerable<TResult> SelectSafe<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)

返回值: 投影后的集合

安全升序排序(支持 null 集合)。

public static IEnumerable<TSource> OrderBySafe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

返回值: 排序后的集合

安全降序排序(支持 null 集合)。

public static IEnumerable<TSource> OrderByDescendingSafe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

返回值: 降序排序后的集合

安全反转集合(支持 null 集合)。

public static IEnumerable<T> ReverseSafe<T>(this IEnumerable<T> source)

返回值: 反转后的集合

随机打乱集合元素顺序。

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)

返回值: 随机排序后的集合

示例:

var list = Enumerable.Range(1, 10).Shuffle().ToList();
// 结果:随机顺序的 1-10

移除集合中的 null 元素。

public static IEnumerable<T> RemoveNulls<T>(this IEnumerable<T> source)

返回值: 不含 null 的集合

示例:

var list = new List<string?> { "a", null, "b", null, "c" };
var nonNull = list.RemoveNulls().ToList(); // ["a", "b", "c"]

集合分页。

public static IEnumerable<T> Page<T>(this IEnumerable<T> source, int pageIndex, int pageSize)

参数:

  • source: 集合
  • pageIndex: 页索引(从 0 开始)
  • pageSize: 每页大小

返回值:

  • 分页后的集合

示例:

var list = Enumerable.Range(1, 100);
var page1 = list.Page(0, 10); // 第1页,包含1-10
var page2 = list.Page(1, 10); // 第2页,包含11-20

集合求和(安全)。

public static int SumSafe(this IEnumerable<int> source)
public static double SumSafe(this IEnumerable<double> source)
public static decimal SumSafe(this IEnumerable<decimal> source)
public static float SumSafe(this IEnumerable<float> source)

参数:

  • source: 集合

返回值:

  • 集合元素之和,如果集合为 null 则返回 0

示例:

List<int> list1 = null;
List<int> list2 = new List<int> { 1, 2, 3 };
int sum1 = list1.SumSafe(); // 0
int sum2 = list2.SumSafe(); // 6

集合平均值(安全)。

public static double AverageSafe(this IEnumerable<int> source)
public static double AverageSafe(this IEnumerable<double> source)

参数:

  • source: 集合

返回值:

  • 集合元素平均值,如果集合为 null 或空则返回 0

示例:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };
double avg = list.AverageSafe(); // 3

获取集合中最大值对应的元素(安全)。

public static TSource MaxBySafe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
where TKey : IComparable<TKey>

参数:

  • source: 集合
  • keySelector: 键选择器

返回值:

  • 最大值对应的元素,如果集合为空则返回默认值

示例:

var list = new List<Person>
{
new Person { Id = 1, Age = 20 },
new Person { Id = 2, Age = 30 },
new Person { Id = 3, Age = 25 }
};
var oldest = list.MaxBySafe(x => x.Age); // Id=2, Age=30

获取集合中最小值对应的元素(安全)。

public static TSource MinBySafe<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
where TKey : IComparable<TKey>

返回值:

  • 最小值对应的元素,如果集合为空则返回默认值

集合最大值(安全)。

public static int MaxSafe(this IEnumerable<int> source)
public static double MaxSafe(this IEnumerable<double> source)
public static decimal MaxSafe(this IEnumerable<decimal> source)
public static float MaxSafe(this IEnumerable<float> source)

返回值: 集合最大值,如果集合为 null 或空则返回默认值

集合最小值(安全)。

public static int MinSafe(this IEnumerable<int> source)
public static double MinSafe(this IEnumerable<double> source)
public static decimal MinSafe(this IEnumerable<decimal> source)
public static float MinSafe(this IEnumerable<float> source)

返回值: 集合最小值,如果集合为 null 或空则返回默认值

集合遍历执行操作(安全)。

public static void ForEachSafe<T>(this IEnumerable<T> source, Action<T> action)

参数:

  • source: 集合
  • action: 要执行的操作

示例:

List<int> list = new List<int> { 1, 2, 3 };
list.ForEachSafe(x => Console.WriteLine(x));

集合遍历执行异步操作。

public static Task ForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> action)

参数:

  • source: 集合
  • action: 要执行的异步操作

返回值:

  • Task

示例:

List<string> urls = new List<string> { "url1", "url2", "url3" };
await urls.ForEachAsync(async url =>
{
await DownloadAsync(url);
});

集合分块(按指定大小分组,安全)。

public static IEnumerable<IEnumerable<T>> ChunkSafe<T>(this IEnumerable<T> source, int size)

参数:

  • source: 集合
  • size: 每块大小

返回值:

  • 分块后的集合

示例:

var list = Enumerable.Range(1, 10);
var chunks = list.ChunkSafe(3).ToList();
// 结果:[1,2,3], [4,5,6], [7,8,9], [10]

集合分批处理。

public static void Batch<T>(this IEnumerable<T> source, int batchSize, Action<IEnumerable<T>> action)

参数:

  • source: 集合
  • batchSize: 批次大小
  • action: 批次处理操作

示例:

var list = Enumerable.Range(1, 100);
list.Batch(10, batch =>
{
ProcessBatch(batch);
});

集合分批异步处理。

public static async Task BatchAsync<T>(this IEnumerable<T> source, int batchSize, Func<IEnumerable<T>, Task> action)

参数:

  • source: 集合
  • batchSize: 批次大小
  • action: 批次异步处理操作

示例:

var list = Enumerable.Range(1, 100);
await list.BatchAsync(10, async batch =>
{
await ProcessBatchAsync(batch);
});

判断集合是否全部满足条件(安全)。

public static bool AllSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 条件谓词

返回值:

  • 如果集合为 null 或所有元素都满足条件返回 true;否则返回 false

示例:

List<int> list = new List<int> { 2, 4, 6, 8 };
bool result = list.AllSafe(x => x % 2 == 0); // true

判断集合是否有任意元素满足条件(安全)。

public static bool AnySafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)

参数:

  • source: 集合
  • predicate: 条件谓词

返回值:

  • 如果集合不为 null 且有任意元素满足条件返回 true;否则返回 false

示例:

List<int> list = new List<int> { 1, 2, 3 };
bool result = list.AnySafe(x => x > 2); // true

判断两个集合是否相等(顺序和元素都相同,安全)。

public static bool SequenceEqualSafe<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 如果两个集合相等返回 true;否则返回 false

示例:

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3 };
bool result = list1.SequenceEqualSafe(list2); // true

判断两个集合是否包含相同的元素(忽略顺序)。

public static bool ContainsSameElements<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 如果两个集合包含相同的元素返回 true;否则返回 false

示例:

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 3, 2, 1 };
bool result = list1.ContainsSameElements(list2); // true

判断集合是否包含 null 元素。

public static bool ContainsNull<T>(this IEnumerable<T> source)

参数:

  • source: 集合

返回值:

  • 如果集合包含 null 元素返回 true;否则返回 false

示例:

var list = new List<string> { "a", null, "b" };
bool result = list.ContainsNull(); // true

合并两个集合(安全)。

public static IEnumerable<T> ConcatSafe<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 合并后的集合

示例:

var list1 = new List<int> { 1, 2 };
var list2 = new List<int> { 3, 4 };
var result = list1.ConcatSafe(list2); // [1, 2, 3, 4]

合并多个集合。

public static IEnumerable<T> ConcatMany<T>(params IEnumerable<T>[] sources)

参数:

  • sources: 集合数组

返回值:

  • 合并后的集合

示例:

var list1 = new List<int> { 1 };
var list2 = new List<int> { 2 };
var list3 = new List<int> { 3 };
var result = EnumerableExtensions.ConcatMany(list1, list2, list3); // [1, 2, 3]

获取两个集合的交集(安全)。

public static IEnumerable<T> IntersectSafe<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 交集

示例:

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 2, 3, 4 };
var result = list1.IntersectSafe(list2); // [2, 3]

获取两个集合的并集(安全)。

public static IEnumerable<T> UnionSafe<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 并集

示例:

var list1 = new List<int> { 1, 2 };
var list2 = new List<int> { 2, 3 };
var result = list1.UnionSafe(list2); // [1, 2, 3]

获取两个集合的差集(安全)。

public static IEnumerable<T> ExceptSafe<T>(this IEnumerable<T> first, IEnumerable<T> second)

参数:

  • first: 第一个集合
  • second: 第二个集合

返回值:

  • 差集(在第一个集合但不在第二个集合的元素)

示例:

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 2, 3, 4 };
var result = list1.ExceptSafe(list2); // [1]

将集合元素连接为字符串。

public static string JoinToString<T>(this IEnumerable<T> source, string separator = ",")
public static string JoinToString<T>(this IEnumerable<T> source, Func<T, string> selector, string separator = ",")

参数:

  • source: 集合
  • separator: 分隔符,默认为逗号
  • selector: 字符串选择器(可选)

返回值:

  • 连接后的字符串

示例:

var list = new List<int> { 1, 2, 3 };
string result1 = list.JoinToString(); // "1,2,3"
string result2 = list.JoinToString(x => $"#{x}", " | "); // "#1 | #2 | #3"

集合遍历执行操作(带索引)。

public static void ForEachIndexed<T>(this IEnumerable<T> source, Action<T, int> action)

参数:

  • source: 集合
  • action: 操作委托,参数为元素和索引

示例:

var list = new List<string> { "a", "b", "c" };
list.ForEachIndexed((item, index) => Console.WriteLine($"{index}: {item}"));
// 输出: 0: a, 1: b, 2: c

安全添加元素到集合。

public static void AddSafe<T>(this ICollection<T> collection, T item)

参数:

  • collection: 集合
  • item: 要添加的元素

示例:

List<int> list = null;
list.AddSafe(1); // 不会抛出异常

安全移除元素。

public static void RemoveSafe<T>(this ICollection<T> collection, T item)

参数:

  • collection: 集合
  • item: 要移除的元素

示例:

var list = new List<int> { 1, 2, 3 };
list.RemoveSafe(2); // [1, 3]

安全清空集合。

public static void ClearSafe<T>(this ICollection<T> collection)

示例:

List<int> list = null;
list.ClearSafe(); // 不会抛出异常

批量添加元素(安全)。

public static void AddRangeSafe<T>(this ICollection<T> collection, IEnumerable<T> items)

参数:

  • collection: 集合
  • items: 要添加的元素集合

示例:

List<int> list = new List<int> { 1, 2 };
list.AddRangeSafe(new[] { 3, 4, 5 });
// list: [1, 2, 3, 4, 5]

批量移除元素(安全)。

public static void RemoveRangeSafe<T>(this ICollection<T> collection, IEnumerable<T> items)

参数:

  • collection: 集合
  • items: 要移除的元素集合

示例:

var list = new List<int> { 1, 2, 3, 4, 5 };
list.RemoveRangeSafe(new[] { 2, 4 });
// list: [1, 3, 5]

  1. 数据处理 - 安全处理可能为空的集合数据,避免 null 异常
  2. Web API - 处理请求和响应数据的集合操作
  3. 数据转换 - 在各种数据结构(DataTable、Dictionary、ConcurrentDictionary)之间转换
  4. 分页处理 - 对数据集合进行分页处理
  5. 统计分析 - 对集合数据进行求和、平均值、最大值等统计计算
  6. 业务逻辑 - 过滤、排序、分组等业务操作
  7. 并发处理 - 转换为线程安全的 ConcurrentDictionary
  8. 数据清洗 - 去重、去除 null 值、批量处理等

  1. 所有方法都是扩展方法,需要通过集合实例调用
  2. 带有 “Safe” 后缀的方法对 null 值做了安全处理,不会抛出异常
  3. 大部分方法在集合为 null 时会返回空集合或默认值,而不是抛出异常
  4. Page<T>() 方法使用 0 作为起始页索引
  5. Chunk<T>() 方法使用延迟执行,内存效率较高
  6. 统计方法在集合为空时会返回默认值(通常为 0),而不是抛出异常
  7. 转换方法在选择器委托为 null 时,使用默认委托进行处理
  8. 集合操作方法都进行了空值有效性检查,使代码更健壮