Skip to content

Instantly share code, notes, and snippets.

@HakanL
Created January 12, 2016 22:09
Show Gist options
  • Save HakanL/14e2272bdf1bc4398da8 to your computer and use it in GitHub Desktop.
Save HakanL/14e2272bdf1bc4398da8 to your computer and use it in GitHub Desktop.
Chunk/batch a list of items
using System;
using System.Collections.Generic;
using System.Linq;
public static class Extentions
{
public static List<List<T>> ChunkBy<T>(this ICollection<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
}
@divinci
Copy link

divinci commented Aug 1, 2018

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment