Skip to content

Instantly share code, notes, and snippets.

@pilotgeraldb
Created June 1, 2016 14:07
Show Gist options
  • Save pilotgeraldb/e0e63c33ba3f73cb7ca2f994569cfa34 to your computer and use it in GitHub Desktop.
Save pilotgeraldb/e0e63c33ba3f73cb7ca2f994569cfa34 to your computer and use it in GitHub Desktop.
splits an ienumerable into groups of count n
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int countPerList)
{
if (source == null || countPerList <= 0)
{
return Enumerable.Empty<IEnumerable<T>>();
}
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / countPerList)
.Select(x => x.Select(v => v.Value).ToList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment