如何分组一个viewbag列表结果?

时间:2019-03-27 15:49:29

标签: asp.net-mvc

我有一个要按年份分组的结果列表。 在第二个foreach中,它显示所有记录。 如何按年份分组?

控制器:

    public ActionResult Summary()
            {
                ViewBag.getdates = db.Database.SqlQuery<CtSummary>(@"  select response_id, show_date,
                                                                                        DATEPART(month, month_year_winners) as month, 
                                                                                        DATEPART(year,month_year_winners) as year 
                                                                                        from table... ").ToList();

                return View();
            }

视图:

   <thead class="thead-light">
      <tr>
         <th>Month</th>
      </tr>
   </thead>
   <tbody>
      @{
      int currentYear = DateTime.Now.Year;
      foreach (var group in ViewBag.getdates)
      {
   <thead>
      <th>@group.year</th>
   </thead>
   foreach (var item in ViewBag.getdates)
   {

   <tr>
      <td>@item.month/@item.year</td>
   </tr>
   }
   }
   }
   </tbody>
</table>```

0 个答案:

没有答案