将集合分割成多个小集合
/// <summary>
/// 将集合进行分割
/// 当list.count小于180000时,CutData耗时短
/// list.count大于180000时,Skip((pageIndex-1)*pageSize).Take(pageSize)耗时短
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="avg"></param>
/// <returns></returns>
public static List<List<T>> CutData<T>(List<T> list, int avg = 2000)
{
List<List<T>> result = new List<List<T>>();
if (list.Count<=avg)
{
result.Add(list);
return result;
}
int length = list.Count;
List<T> tempList = new List<T>();
for (int i = 0; i < length; i++)
{
tempList.Add(list[i]);
if ((i + 1) % avg == 0)
{
result.Add(tempList);
tempList = new List<T>();
continue;
}
if ((i + 1) == length)
{
result.Add(tempList);
tempList =null;
}
}
return result;
}
一直以来,更新数据库时,数量不能太大,需要将一个大的集合分割成多个小的集合,才有了上面的方法
浙公网安备 33010602011771号