Created
October 21, 2016 20:42
-
-
Save aaronhoffman/02e29a8869e46a6f5094ed3f16426f08 to your computer and use it in GitHub Desktop.
LINQ Batch Enumerable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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