备份一个集合分组的算法
1 /// <summary> 2 /// 集合分组 3 /// </summary> 4 /// <param name="array">需要分组的集合</param> 5 /// <param name="size">每个分组的大小</param> 6 /// <returns></returns> 7 public static T[][] GroupArray<T>(T[] array, int size = 10) 8 { 9 List<T[]> lst = new List<T[]>(); 10 int count = array.Length % size == 0 ? array.Length / size : array.Length / size + 1; 11 for (int i = 0, length = count; i < length; i++) 12 { 13 var arr = array.Skip(i * size).Take(size); 14 lst.Add(arr.ToArray()); 15 } 16 return lst.ToArray(); 17 }
/// <summary> /// 集合分组 /// </summary> /// <param name="array">需要分组的集合</param> /// <param name="size">每个分组的大小</param> /// <returns></returns> public static T[][] GroupArray<T>(this IEnumerable<T> array, int size = 10) { var arrLength = array.Count(); List<T[]> lst = new List<T[]>(); int count = arrLength % size == 0 ? arrLength / size : arrLength / size + 1; for (int i = 0, length = count; i < length; i++) { var arr = array.Skip(i * size).Take(size); lst.Add(arr.ToArray()); } return lst.ToArray(); }