Split a List into smaller lists of N size

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

    public static List> ChunkBy(this List source, int chunkSize)
        {           
            var result = new List>();
            for (int i = 0; i < source.Count; i += chunkSize)
            {
                var rows = new List();
                for (int j = i; j < i + chunkSize; j++)
                {
                    if (j >= source.Count) break;
                    rows.Add(source[j]);
                }
                result.Add(rows);
            }
            return result;
        }
    

提交回复
热议问题