数组、IEnumerable、List、合分片或分块
/// <summary> /// /// </summary> public static class IEnumerableExtentions { /// <summary> /// 复制序列中的数据 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="iEnumberable">原数据</param> /// <param name="startIndex">原数据开始复制的起始位置</param> /// <param name="length">需要复制的数据长度</param> /// <returns></returns> public static IEnumerable<T> Copy<T>(this IEnumerable<T> iEnumberable, int startIndex, int length) { var sourceArray = iEnumberable.ToArray(); T[] newArray = new T[length]; Array.Copy(sourceArray, startIndex, newArray, 0, length); return newArray; } /// <summary> /// 给IEnumerable拓展ForEach方法 /// </summary> /// <typeparam name="T">模型类</typeparam> /// <param name="iEnumberable">数据源</param> /// <param name="func">方法</param> public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T> func) { foreach (var item in iEnumberable) { func(item); } } /// <summary> /// 给IEnumerable拓展ForEach方法 /// </summary> /// <typeparam name="T">模型类</typeparam> /// <param name="iEnumberable">数据源</param> /// <param name="func">方法</param> public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T, int> func) { var array = iEnumberable.ToArray(); for (int i = 0; i < array.Count(); i++) { func(array[i], i); } } /// <summary> /// 分块 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="chunkSize"></param> /// <returns></returns> public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize) { return source .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / chunkSize) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); }
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET