我正在尝试输出一些数据,分组,然后再次分组。
这是一些示例数据(来自 db)
我正在尝试输出这个,分组如下:
Best Type Name
- Discipline name
-- Result
-- Result
-- Result
CompetitorBest 类看起来像:
public class CompetitorBest
{
public int ResultId { get; set; }
public string BestTypeName { get; set; }
public int BestTypeOrder { get; set; }
public string DisciplineName { get; set; }
public string ResultValue { get; set; }
public string Venue { get; set; }
public DateTime ResultDate { get; set; }
}
目前,我有以下
var bestsGroups = from b in Model.CompetitorBests
group b by new { b.BestTypeName, b.BestTypeOrder }
into grp
orderby grp.Key.BestTypeOrder
select new
{
BestType = grp.Key.BestTypeName,
Results = grp.ToList()
};
但这显然没有考虑到 DisciplineName 的分组。
我的输出代码类似于:
foreach (var bestsGroup in bestsGroups)
{
<h2>@bestsGroup.BestType</h2>
foreach (var result in bestsGroup.Results)
{
//i am guessing here i'll need another foreach on the discipline group....
<p>@result.DisciplineName</p>
<p>@result.ResultId </p>
}
}