7

グループ化されたデータを出力してから、再度グループ化しようとしています。

ここにいくつかのサンプルデータがあります(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>
    }
}
4

1 に答える 1

8

これがあなたが探しているものだと思います:

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 = from d in grp
              group d by d.DisciplineName into grp2
              select new
              {
                  DisciplineName = grp2.Key,
                  Results = grp2
              }
};

編集:

次のように繰り返します。

foreach (var bestsGroup in bestsGroups)
{
    <h2>@bestsGroup.BestType</h2>

    foreach (var discipline in bestsGroup.Results)
    {
        <p>@discipline.DisciplineName</p>

        foreach (var result in discipline.Results)
        {
            <p>@result.ResultId</p>
        }
    }
}
于 2012-07-05T11:30:36.607 に答える