using System.Collections.Generic; using System.Linq; namespace BusinessLogic.Extensions { public static class IEnumerableExtensions { public static IEnumerable> Batch( this IEnumerable source, int batchSize) { var batch = new List(1000); foreach (var item in source) { batch.Add(item); if (batch.Count == batchSize) { yield return batch; batch = new List(1000); } } if (batch.Any()) yield return batch; } } }