Created
September 17, 2015 15:09
-
-
Save odytrice/e26370479b470d34b153 to your computer and use it in GitHub Desktop.
Batch Processing Operator for IQueryable and IEnumerables
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; | |
namespace System.Linq | |
{ | |
public static class Extensions | |
{ | |
#region IQueryable Extensions | |
public static IEnumerable<IQueryable<T>> Batch<T>(this IQueryable<T> query, int batchSize) | |
{ | |
int count = query.Count(); | |
int batchCount = count <= batchSize ? 1 : (count % batchSize == 0 ? (count / batchSize) : (count / batchSize + 1)); | |
for (int i = 0; i < batchCount; i++) | |
yield return query.Skip(i * batchSize).Take(batchSize); | |
} | |
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> query, int batchSize) | |
{ | |
int count = query.Count(); | |
int batchCount = count <= batchSize ? 1 : (count % batchSize == 0 ? (count / batchSize) : (count / batchSize + 1)); | |
for (int i = 0; i < batchCount; i++) | |
yield return query.Skip(i * batchSize).Take(batchSize); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment