using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudBuilder.Core.Extensions { /// /// Enumerable集合扩展方法 /// public static class EnumerableExtensions { /// /// 打乱一个集合的项顺序 /// public static IEnumerable Shuffle(this IEnumerable source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return source.OrderBy(m => Guid.NewGuid()); } /// /// 将集合展开并分别转换成字符串,再以指定的分隔符衔接,拼成一个字符串返回。默认分隔符为逗号 /// /// 要处理的集合 /// 分隔符,默认为逗号 /// 拼接后的字符串 public static string ExpandAndToString(this IEnumerable collection, string separator = ",") { return collection.ExpandAndToString(t => t?.ToString(), separator); } /// /// 循环集合的每一项,调用委托生成字符串,返回合并后的字符串。默认分隔符为逗号 /// /// 待处理的集合 /// 单个集合项的转换委托 /// 分隔符,默认为逗号 /// 泛型类型 /// public static string ExpandAndToString(this IEnumerable collection, Func itemFormatFunc, string separator = ",") { collection = collection as IList ?? collection.ToList(); if (!collection.Any()) { return string.Empty; } StringBuilder sb = new StringBuilder(); int i = 0; int count = collection.Count(); foreach (T t in collection) { if (i == count - 1) { sb.Append(itemFormatFunc(t)); } else { sb.Append(itemFormatFunc(t) + separator); } i++; } return sb.ToString(); } /// /// 集合是否为空 /// /// 要处理的集合 /// 动态类型 /// 为空返回True,不为空返回False public static bool IsEmpty(this IEnumerable collection) { collection = collection as IList ?? collection.ToList(); return !collection.Any(); } /// /// 根据第三方条件是否为真来决定是否执行指定条件的查询 /// /// 要查询的源 /// 查询条件 /// 第三方条件 /// 动态类型 /// 查询的结果 public static IEnumerable WhereIf(this IEnumerable source, Func predicate, bool condition) { source = source as IList ?? source.ToList(); return condition ? source.Where(predicate) : source; } /// /// 将字符串集合按指定前缀排序 /// public static IEnumerable OrderByPrefixes(this IEnumerable source, Func keySelector, params string[] prefixes) { List all = source.OrderBy(keySelector).ToList(); List result = new List(); foreach (string prefix in prefixes) { List tmpList = all.Where(m => keySelector(m).StartsWith(prefix)).OrderBy(keySelector).ToList(); all = all.Except(tmpList).ToList(); result.AddRange(tmpList); } result.AddRange(all); return result; } /// /// 根据指定条件返回集合中不重复的元素 /// /// 动态类型 /// 动态筛选条件类型 /// 要操作的源 /// 重复数据筛选条件 /// 不重复元素的集合 public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) { source = source as IList ?? source.ToList(); return source.GroupBy(keySelector).Select(group => group.First()); } /// /// 把集合按指定字段与排序方式进行排序 /// /// 集合项类型 /// 要排序的数据集 /// 排序属性名 /// 排序方向 /// 排序后的数据集 public static IOrderedEnumerable OrderBy(this IEnumerable source, string propertyName, ListSortDirection sortDirection = ListSortDirection.Ascending) { return CollectionPropertySorter.OrderBy(source, propertyName, sortDirection); } /// /// 把集合按指定字段排序条件进行排序 /// /// 动态类型 /// 要排序的数据集 /// 列表字段排序条件 /// public static IOrderedEnumerable OrderBy(this IEnumerable source, SortCondition sortCondition) { return source.OrderBy(sortCondition.SortField, sortCondition.ListSortDirection); } /// /// 把集合按指定字段排序条件进行排序 /// /// 动态类型 /// 要排序的数据集 /// 列表字段排序条件 /// public static IOrderedEnumerable OrderBy(this IEnumerable source, SortCondition sortCondition) { return source.OrderBy(sortCondition.SortField, sortCondition.ListSortDirection); } /// /// 把集合继续按指定字段排序方式进行排序 /// /// 动态类型 /// 要排序的数据集 /// 排序属性名 /// 排序方向 /// public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, string propertyName, ListSortDirection sortDirection = ListSortDirection.Ascending) { propertyName.CheckNotNullOrEmpty("propertyName"); return CollectionPropertySorter.ThenBy(source, propertyName, sortDirection); } /// /// 把集合继续指定字段排序方式进行排序 /// /// 动态类型 /// 要排序的数据集 /// 列表字段排序条件 /// public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, SortCondition sortCondition) { return source.ThenBy(sortCondition.SortField, sortCondition.ListSortDirection); } } }