Skip to content

Instantly share code, notes, and snippets.

@adamhopkinson
Created June 24, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamhopkinson/0ef7bfca02678f12a841 to your computer and use it in GitHub Desktop.
Save adamhopkinson/0ef7bfca02678f12a841 to your computer and use it in GitHub Desktop.
Breaking a list into chunks
// define how many items we want per chunk
var itemsPerChunk = 3;
// break the list of items into chunks
var chunks = items.Select((s, i) => new { Value = s, Index = i }).GroupBy(item => item.Index / itemsPerChunk, item => item.Value);
// iterate through each chunk
foreach (var chunk in chunks)
{
<div class="row">
// iterate through each item in the chunk
@foreach (var item in chunk)
{
<div class="span4">
@item.DisplayName
</div>
}
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment