Skip to content

Instantly share code, notes, and snippets.

@AThraen
Created October 2, 2022 18:03
Show Gist options
  • Select an option

  • Save AThraen/ee06d7ef18f149c7109881d54a1124e7 to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/ee06d7ef18f149c7109881d54a1124e7 to your computer and use it in GitHub Desktop.
Chunkify, breaks up a generic enumerator into batches (chunks) of a given size
protected IEnumerable<List<G>> Chunkify<G>(IEnumerable<G> Source,int ChunkSize)
{
List<G> chunk=new List<G>(ChunkSize);
foreach(var e in Source)
{
chunk.Add(e);
if(chunk.Count > ChunkSize)
{
yield return chunk;
chunk.Clear();
}
}
if (chunk.Any()) yield return chunk;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment