using System.Collections; using System.Text; using JetBrains.Annotations; namespace DS.Module.Core.Extensions; public static partial class Extensions { /// /// 根据Index下标移除 /// /// /// /// /// public static List Remove(this List list, int index) where T : class, new() { list.RemoveAt(index); return list; } /// /// 移除 /// /// /// /// public static List RemoveAll(this List list) where T : class, new() { list.NotNullOrEmpty(nameof(list)); for (int i = list.Count - 1; i >= 0; i--) { list.RemoveAt(i); } return list; } /// 串联对象数组的各个元素,其中在每个元素之间使用指定的分隔符。 /// 一个由 的元素组成的字符串,这些元素以 字符串分隔。如果 为空数组,该方法将返回 /// 要用作分隔符的字符串。只有在 具有多个元素时, 才包括在返回的字符串中。 /// 一个集合,其中包含要连接的元素。 /// /// 为 null。 public static string ToJoin(this IEnumerable values, string separator = ",") where TSource : IEnumerable { values = values.Where(o => !o.AsTo().IsNullOrEmpty()); return string.Join(separator, values); } /// /// 去重 /// /// 去重数据源 /// /// 数据源 /// 键条件 /// public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) { return source.GroupBy(keySelector).Select(gropby => gropby.First()); } /// /// 去重 /// /// 去重数据源 /// /// 数据源 /// 键条件 /// 返回去重后集合数据 public static IList ToDistinctBy(this IEnumerable source, Func keySelector) { return source.DistinctBy(keySelector).ToList(); } /// /// 把集合转成SqlIn /// /// /// 要转换的值 /// 分割符 /// 左边符 /// 右边符 /// 返回组装好的值,例如"'a','b'" public static string ToSqlIn(this IEnumerable values, string separator = ",", string left = "'", string right = "'") { StringBuilder sb = new StringBuilder(); var enumerable = values as TSource[] ?? values.ToArray(); if (!enumerable.Any()) { return string.Empty; } enumerable.ToList().ForEach(o => { sb.AppendFormat("{0}{1}{2}{3}", left, o, right, separator); }); string newStr = sb.ToString()?.TrimEnd($"{separator}".ToCharArray()); return newStr; } /// /// 根据集合字典转成字典 /// /// 键的类型 /// 值的类型 /// 数据源 /// 返回所需的字典 public static IDictionary AsDictionary( this IEnumerable> keyValuePairs) { keyValuePairs.NotNullOrEmpty(nameof(keyValuePairs)); IDictionary dic = new Dictionary(); foreach (KeyValuePair keys in keyValuePairs) { dic.Add(keys.Key, keys.Value); } return dic; } public static IEnumerable WhereIf(this IEnumerable source, Func predicate, bool condition) where TSource : IEnumerable { source.NotNullOrEmpty(nameof(source)); predicate.NotNull(nameof(predicate)); return condition ? source.Where(predicate) : source; } /// /// 给IEnumerable拓展ForEach方法 /// /// 模型类 /// 数据源 /// 方法 public static void ForEach(this IEnumerable iEnumberable, Action func) { foreach (var item in iEnumberable) { func(item); } } /// /// 给IEnumerable拓展ForEach方法 /// /// 模型类 /// 数据源 /// 方法 public static void ForEach(this IEnumerable iEnumberable, Action func) { var array = iEnumberable.ToArray(); for (int i = 0; i < array.Count(); i++) { func(array[i], i); } } /// /// 将列表转换为树形结构(泛型无限递归) /// /// 类型 /// 数据 /// 根条件 /// 节点条件 /// 添加子节点 /// /// public static List ToTree(this List list, Func rootwhere, Func childswhere, Action> addchilds, T entity = default(T)) { var treelist = new List(); //空树 if (list == null || list.Count == 0) { return treelist; } if (!list.Any(e => rootwhere(entity, e))) { return treelist; } //树根 if (list.Any(e => rootwhere(entity, e))) { treelist.AddRange(list.Where(e => rootwhere(entity, e))); } //树叶 foreach (var item in treelist) { if (list.Any(e => childswhere(item, e))) { var nodedata = list.Where(e => childswhere(item, e)).ToList(); foreach (var child in nodedata) { //添加子集 var data = list.ToTree(childswhere, childswhere, addchilds, child); addchilds(child, data); } addchilds(item, nodedata); } } return treelist; } /// /// 把集合的元素转成指定的类型 /// /// 要转换的类型 /// 转换的数据源 /// 返回转换后的集合 public static IEnumerable AsToAll(this IEnumerable source) { source.NotNull(nameof(source)); IEnumerable enumerable = source as IEnumerable; if (enumerable != null) { return enumerable; } return CastIterator(source); } private static IEnumerable CastIterator(IEnumerable source) { foreach (object current in source) { yield return (current.AsTo()); } yield break; } public static bool AddIfNotContains([NotNull] this ICollection source, T item) { source.NotNull(nameof(source)); if (source.Contains(item)) { return false; } source.Add(item); return true; } }