Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created June 14, 2011 02:59
Show Gist options
  • Save juanplopes/1024221 to your computer and use it in GitHub Desktop.
Save juanplopes/1024221 to your computer and use it in GitHub Desktop.
BatchAggregate
public static IEnumerable<IList<T>> BatchAggregate<T>(this IEnumerable<T> source, int batchSize)
{
var list = new List<T>(batchSize);
foreach(var item in source)
{
list.Add(item);
if (list.Count == batchSize)
{
yield return list;
list = new List<T>(batchSize);
}
}
if (list.Any())
yield return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment