How can I group items in a table? I am bringing these items from the database, I would like to group by the code of the same, for example, if there is repeated code 1 would like to group it into a single item. This is the controller class. This is the Controller I'm using.
// GET: PesquisaProdutoes
public ActionResult RelatorioPConfirmado()
{
var pesquisa = db.PesquisaProdutoes.GroupBy(c => c.CodPro);
return View(pesquisa.ToList());
}
Below is the CSHTML preview file.
<table class="table table-bordered" id="dataTable" cellspacing="0">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.CodPro)</th>
<th>@Html.DisplayNameFor(model => model.CodBar)</th>
<th>@Html.DisplayNameFor(model => model.Nome)</th>
<th>@Html.DisplayNameFor(model => model.Valor)</th>
<th>@Html.DisplayNameFor(model => model.Qtd)</th>
<th>@Html.DisplayNameFor(model => model.Total)</th>
<th>@Html.DisplayNameFor(model => model.Confimado)</th>
<th>@Html.DisplayNameFor(model => model.DataPesquisa)</th>
</thead>
@foreach (var item in Model)
{
if (item.Confimado.Equals(true))
{
<tr>
<td>@Html.DisplayFor(modelItem => item.CodPro)</td>
<td>@Html.DisplayFor(modelItem => item.CodBar)</td>
<td>@Html.DisplayFor(modelItem => item.Nome)</td>
<td>@Model.Sum(v => v.Valor)</td>
<td>@Model.Sum(q => q.Qtd)</td>
<td>@Model.Sum(t => t.Total)</td>
<td>@Html.DisplayFor(modelItem => item.Confimado)</td>
<td>@Html.DisplayFor(modelItem => item.DataPesquisa)</td>
</tr>
}
}
</table>
This is my Data model.
[Display(Name = "Codigo Produto")]
public int CodPro { get; set; }
[Display(Name = "Codigo de Barras")]
public string CodBar { get; set; }
[Display(Name = "Nome Produto")]
public string Nome { get; set; }
[Display(Name = "Valor unitário")]
public decimal Valor { get; set; }
[Display(Name = "Quantidade informada")]
public decimal Qtd { get; set; }
[Display(Name = "Valor Total")]
public decimal Total { get; set; }
[Display(Name = "Produto foi confirmado?")]
public bool Confimado { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DataPesquisa { get; set; } = DateTime.Now;