Could I suggest that you use the Linq GroupBy
method? Something along these lines. I have assumed that the Product
class has a Name
property.
@{
var groups = Model.GroupBy(
m => m.Category, //key
m => m.Product,//collection 'group'
(key, g) => new { Category = key, Products= g });
}
@foreach (var group in groups)
{
<h2>@group.Category</h2>
@foreach (var product in group.Products)
{
<p>@product.Name</p>
}
}