Split List in C# with a batch size N

Hi All,

Sometimes we split List into batches based on our requirement. Use below code to split List into batches of Size N

public IEnumerable<List<T>> SplitList<T>(List<T> locations, int nSize = N)
{
for (int i = 0; i < locations.Count; i += nSize)
{
yield return locations.GetRange(i, Math.Min(nSize, locations.Count – i));
}
}

Here N is of Data Type int and we can assign the batch size

Hope this helps šŸ™‚

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s