Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Created October 21, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronhoffman/02e29a8869e46a6f5094ed3f16426f08 to your computer and use it in GitHub Desktop.
Save aaronhoffman/02e29a8869e46a6f5094ed3f16426f08 to your computer and use it in GitHub Desktop.
LINQ Batch Enumerable
using System.Collections.Generic;
using System.Linq;
public static class EnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
{
return items
.Select((item, inx) => new { item, inx })
.GroupBy(x => x.inx / batchSize)
.Select(g => g.Select(x => x.item));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment