Did you ever need to take a collection of items and break it into a set of batches, or divide those items into partitions? Well here are some simple extension methods to help you do that:
Extension Methods
public static class EnumerableExtensions
{
public static IList<IList<T>> Partition<T>(
this IEnumerable<T> items,
int partitionCount)
{
if (partitionCount == 0)
{
throw new ArgumentException(
"Partition Count must be greater than zero",
"partitionCount");
}
return items
.Select(
(v, i) => new
{
Group = i%partitionCount,
Value = v
})
.GroupBy(k => k.Group, v => v.Value)
.Select(g => (IList<T>) g.ToList())
.ToList();
}
public static IList<IList<T>> Batch<T>(
this IEnumerable<T> items,
int batchSize)
{
if (batchSize == 0)
{
throw new ArgumentException(
"Batch Size must be greater than zero",
"batchSize");
}
var batches = new List<IList<T>>();
var batch = new List<T>();
foreach (var item in items)
{
if (batch.Count == batchSize)
{
batches.Add(batch);
batch = new List<T>();
}
batch.Add(item);
}
if (batch.Count > 0)
{
batches.Add(batch);
}
return batches;
}
}