Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Last active January 19, 2021 12:38
Show Gist options
  • Select an option

  • Save skalahonza/31d4d78146ac8f18d54463f3408db9c2 to your computer and use it in GitHub Desktop.

Select an option

Save skalahonza/31d4d78146ac8f18d54463f3408db9c2 to your computer and use it in GitHub Desktop.
How to split huge payload into multiple batches
public static class IEnumerableExtensions
{
/// <summary>
/// Split source into batches of limited size
/// </summary>
/// <param name="limit">Batch size limit</param>
/// <returns></returns>
public static IEnumerable<T[]> Batchify<T>(this IEnumerable<T> source, int limit)
{
var batch = source.Take(limit).ToArray();
if (batch.Length > 0) yield return batch;
if (batch.Length == limit)
foreach (var b in source.Skip(limit).Batchify(limit))
yield return b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment