我的班级名称为Fuel
,其中包含以下数据:
这是我的 Model 类:
public class Fuel
{
public int FuelID { get; set; }
public string Name { get; set; }
public DateTime OrderDate { get; set; }
public double Qty { get; set; }
}
表中的数据。
Name | OrderDate | Qty
H&S1 | 1/1/2008 | 100
H&S1 | 1/2/2008 | 200
H&S1 | 2/2/2008 | 350
H&S2 | 2/28/2008 | 220
H&S2 | 3/12/2008 | 250
H&S2 | 3/15/2008 | 2150
IActionResult 如下:
public IActionResult GetRes()
{
List<Fuel> myList = List<Fuel>();
var query = myList
.GroupBy(c => c.Name)
.Select(g => new {
Name= g.Key,
Jan = g.Where(c => c.OrderDate.Month == 1).Sum(c => c.Qty),
Feb = g.Where(c => c.OrderDate.Month == 2).Sum(c => c.Qty),
March = g.Where(c => c.OrderDate.Month == 3).Sum(c => c.Qty)
});
return View(myList);
}
这是我的视图
@model IEnumerable<CoreServices.Model.Fuel>
@{
Layout = null;
}
<div class="row">
<table>
<tr>
<td>Name</td>
<td>Date</td>
<td>Quantity</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Date</td>
<td>@item.Qty</td>
</tr>
}
</table>
</div>
但结果显示类似
Name | OrderDate | Qty
H&S1 | 1/1/2008 | 100
H&S1 | 1/2/2008 | 200
H&S1 | 2/2/2008 | 350
H&S2 | 2/28/2008 | 220
H&S2 | 3/12/2008 | 250
H&S2 | 3/15/2008 | 2150
我想像这样在 View 上显示结果:
Name| Jan- 2008 | Feb- 2008 | Mar - 2008| Total
H&S1| 100 | 350 | 250 | 700
H&S2| 200 | 220 | 2150 | 2570
有人可以指导我如何在View上显示以上格式吗?