Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created August 9, 2017 14:33
Show Gist options
  • Select an option

  • Save sandcastle/153d039c9580992f943c68bb0ef8d999 to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/153d039c9580992f943c68bb0ef8d999 to your computer and use it in GitHub Desktop.
Partition a list into many partitions of equal size. Handy for things like migrations where you want to break things into "buckets"
internal static class LinqExtensions
{
public static List<List<T>> Partition<T>(this IEnumerable<T> list, int partitionSize)
{
var i = 0;
var splits = from item in list
group item by i++ / partitionSize into part
select part.ToList();
return splits.ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment