Last active
January 19, 2021 12:38
-
-
Save skalahonza/31d4d78146ac8f18d54463f3408db9c2 to your computer and use it in GitHub Desktop.
How to split huge payload into multiple batches
This file contains hidden or 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
| 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