I have a table, where I get the data from a View (SQL Server) and perform some operations on my page. On this page, I have a filter, to display only the pages for the selected year.
Until this part is all correct, along with the filter, such as seen in this example .
The operations are performed, entirely in my View, which follows below:
<table id="item-list" class="item-list table" style="margin-bottom: 0px;" border="1">
<thead>
<tr>
<th rowspan="2">
Mês
</th>
<th rowspan="2">
Remuneração<br />
Total R$
</th>
<th colspan="4">
<p align="center">
Contribuinte
</p>
</th>
<th colspan="4">
<p align="center">
Município
</p>
</th>
</tr>
<tr bgcolor="#ffffff">
<th>
%
</th>
<th>
Mês R$
</th>
<th>
Ano R$
</th>
<th>
Acumulado R$
</th>
<th>
%
</th>
<th>
Mês R$
</th>
<th>
Ano R$
</th>
<th>
Acumulado R$
</th>
</tr>
</thead>
<tbody>
@*Variáveis para somar os totais dos campos*@
@{
double totalContribuinte = 0;
double totalMunicipio = 0;
}
@foreach (var item in Model.Previdencia.GroupBy(g => new {g.NmPessoa, g.dtCompetencia.Value.Year}))
{
@*Variáveis para somar os totais dos campos por ano*@
double subtotalContribuinte = 0;
double subtotalMunicipio = 0;
foreach (var contribuicoes in item.ToList())
{
@*Realizam as somas dos campos*@
subtotalContribuinte += contribuicoes.Contribuinte;
totalContribuinte += contribuicoes.Contribuinte;
subtotalMunicipio += contribuicoes.BaseCalculo;
totalMunicipio += contribuicoes.BaseCalculo;
@*Adiciono a classe igual a selecionada no select*@
<tr class="@contribuicoes.dtCompetencia.Value.Year" bgcolor="#ffffff">
<td align="right">
@contribuicoes.dtCompetencia.Value.Month.ToString("00")
</td>
<td align="right">
@contribuicoes.BaseCalculo.ToString("N")
</td>
<td align="right">
11
</td>
<td align="right">
@contribuicoes.Contribuinte.ToString("N")
</td>
<td align="right">
@subtotalContribuinte.ToString("N")
</td>
<td align="right">
@totalContribuinte.ToString("N")
</td>
<td align="right">
11
</td>
<td align="right">
@contribuicoes.BaseCalculo.ToString("N")
</td>
<td align="right">
@subtotalMunicipio.ToString("N")
</td>
<td align="right">
@totalMunicipio.ToString("N")
</td>
</tr>
}
}
</tbody>
</table>
My ViewModel
used, with fields:
public class ExtratoPrevidenciaViewModel
{
public int Codigo { get; set; }
public int CdMatricula { get; set; }
public Int16 SqContrato { get; set; }
public string NmPessoa { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dtCompetencia { get; set; }
public double Contribuinte { get; set; }
public double Municipio { get; set; }
public double BaseCalculo { get; set; }
}
My problem: How to perform the sum of all values, per column, separating by year, how is my GroupBy
?
The expected end result, would look something like this.