Created
June 1, 2016 14:07
-
-
Save pilotgeraldb/e0e63c33ba3f73cb7ca2f994569cfa34 to your computer and use it in GitHub Desktop.
splits an ienumerable into groups of count n
This file contains hidden or 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
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