Split a List into smaller lists of N size

前端 未结 17 1646
后悔当初
后悔当初 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:51

    One more

    public static IList> SplitList(this IList list, int chunkSize)
    {
        var chunks = new List>();
        List chunk = null;
        for (var i = 0; i < list.Count; i++)
        {
            if (i % chunkSize == 0)
            {
                chunk = new List(chunkSize);
                chunks.Add(chunk);
            }
            chunk.Add(list[i]);
        }
        return chunks;
    }
    

提交回复
热议问题