In addition to the detail rows in my view, I would like to show some aggregate data like "count" and "sum". I have a picking lists table, and apart from displaying the list lines organized by SKU, I would like to show a total per SKU.
My PickingList model looks like the following:
public partial class PickingList
{
public int Id { get; set; }
[ForeignKey("List_Header")]
public int TransID { get; set; }
public string SKU { get; set; }
public int ColorId { get; set; }
public double Qty { get; set; }
public decimal Price { get; set; }
public virtual List_Header List_Header { get; set; }
}
The controller, where I pass the TransID to the list is:
public ActionResult ListShipment(int transid)
{
var mylist = db.PickingLists.Where(p => p.TransID == transid);
mylist = mylist.OrderBy(p => p.Id);
return View(mylist.ToList());
}
And, finally, what I would like to achieve is something like this:
SKU Qty Color
61009 12 pieces
3 Blue
5 Red
4 Green
61011 10 pieces
6 Blue
4 Red
I've tried the following View code block, but can't figure out where to place the aggregate (sum) function:
@foreach (var group in Model.GroupBy(item => item.SKU))
{
<tr>
<td>
@Html.DisplayFor(modelItem => group.Key)
</td>
</tr>
foreach (var item in group.OrderBy(o => o.Id))
{
<tr>
<td></td>
<td>
@Html.DisplayFor(modelItem => item.Qty)
</td>
.........
.........
.........
How could I achieve that?