Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IJsWorkshop/213466e5850d75cc0cbdd0c066f511a8 to your computer and use it in GitHub Desktop.
Save IJsWorkshop/213466e5850d75cc0cbdd0c066f511a8 to your computer and use it in GitHub Desktop.
Split a List<T> into a List<List<T>> of nth size
public static List<List<T>> ChunkInto<T>(this IList<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();
}
@IJsWorkshop
Copy link
Author

IJsWorkshop commented Jul 2, 2023

Handy function to split/chunk a large List into small nth size Lists - List<List>

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