Splitting a list or collection into chunks in C#

By | December 12, 2020

Problem Statement – 

While working on project, one requirement came like client wants to save 50k of records into db in one go. which was taking lot of time to save long list of data to be proceessed and saved in database. also we had 8 api nodes and this code were running on signle api node, so what we did is slipt the list into chunk of 6k of records and saved data parallely into 8 api nodes, which improved performance 8x times.

Solution – Split / Partition a collection into smaller collections

public static List<List<T>> SplitList<T>(List<T> longList, int nSize = 50)
            var smallLstChunks = new List<List<T>>();
            for (int i = 0; i < longList.Count; i += nSize)
                smallLstChunks.Add(longList.GetRange(i, Math.Min(nSize, longList.Count - i)));
            return smallLstChunks;