Created
April 30, 2015 22:08
-
-
Save jonocairns/4fdfbe629808dd92e0a5 to your computer and use it in GitHub Desktop.
This file contains 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 class ListExtensions | |
{ | |
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize) | |
{ | |
Argument.CheckIfNull(source, "source"); | |
Argument.CheckIfNull(chunkSize, "chunkSize"); | |
if (chunkSize <= 0) | |
{ | |
throw new ArgumentException("chunkSize should be positive", "chunkSize"); | |
} | |
return source | |
.Select((x, i) => new { Index = i, Value = x }) | |
.GroupBy(x => x.Index / chunkSize) | |
.Select(x => x.Select(y => y.Value).ToList()) | |
.ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment