IQueryable<T> extension method to take data in batches
What you can do is this:
public static IEnumerable<IQueryable<T>> InBatches(
this IQueryable<T> collection, int size)
{
int totalSize = collection.Count();
for (int start = 0; start < totalSize; start += size)
{
yield return collection.Skip(start).Take(size);
}
}
This extension method allows you to do extra filters over the return IQueryables. However, the usefulness is pretty limited. I can't think of any good scenario for this :-). In most scenario's you just want to stream the results and returning an IEnumerable<IEnumerable<T>>
would just do fine, and is even better, since this will result in a single SQL query, while the shown approach will result in N + 1 queries.
What's wrong with Take
and Skip
? These are the LINQ operators for getting batches off an IEnumerable<T>
or IQueryable<T>
(and their non-generic counterparts).