Skip to content

@wolfbyte /gist:4016067
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
How to split a list into ‘chunks’ (Fun Code)
void Main()
{
var items = new List<string>{ "A", "B", "C", "D", "E", "F", "G", "H", "I" };
items.Chunk(2).Dump();
}
public static class Ext
{
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize)
{
using(var enumerator = source.GetEnumerator()) while(enumerator.MoveNext()) yield return Take(enumerator, chunkSize);
}
private static IEnumerable<T> Take<T>(IEnumerator<T> enumerator, int n)
{
do yield return enumerator.Current; while(--n > 0 && enumerator.MoveNext());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.