Group By Index

Use the Select(Func<TSource, int, TValue>) overload that projects items into a new form using its index, in this case an anonymous type that has an Index property representing the index / 3, and an Item property, representing the original item's value.

var dataset = Enumerable.Range(0, 20).Select(x => new { Text = "item " + x });
var data = dataset
    .Select((value, index) => new { Index = index / 3, Item = value })
    .GroupBy(pair => pair.Index);
<table>
@foreach (var group in data)
{
    <tr>
        @foreach (var element in group)
        {
            <td>@element.Item.Text</td>
        }
        @for (int i = 0; i < 3 - group.Count(); i++)
        {
            <td>empty</td> 
        }
    </tr>
}
</table>

https://stackoverflow.com/questions/8639387/building-tables-with-webmatrix

On this page

Latest Updates

© 0 - 2025 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com