Split a List into smaller lists of N size

前端 未结 17 1840
后悔当初
后悔当初 2020-11-22 16:55

I am attempting to split a list into a series of smaller lists.

My Problem: My function to split lists doesn\'t split them into lists of the correct

17条回答
  •  礼貌的吻别
    2020-11-22 17:35

    Serj-Tm solution is fine, also this is the generic version as extension method for lists (put it into a static class):

    public static List> Split(this List items, int sliceSize = 30)
    {
        List> list = new List>();
        for (int i = 0; i < items.Count; i += sliceSize)
            list.Add(items.GetRange(i, Math.Min(sliceSize, items.Count - i)));
        return list;
    } 
    

提交回复
热议问题