Restart count when changing year

1

I have a view that returns me data that the taxpayer paid the pension in the current year, where I return the monthly payment of the same. I need to complete the sum of the months, creating a monthly subTotal. With help I managed to work, leaving the result as the example:
JsFiddle Example

But I need the sum to restart at the end of the year. This logic I can not elaborate. I need the final result to do something like:
Example Expected result

My controller looks like this:

public ActionResult Index()
        {
           //realizo a busca no BD
            var usuario =
                previdenciaRepository.Previdencias.Where(p => p.CdMatricula == matricula && p.SqContrato == contrato).ToList();       

            return View(usuario);
        }

And in the view I make the calculation to sum the data returned, thus:

<table border="1">
        <thead>
            <tr>
                <th rowspan="2">
                    Mês
                </th>
                <th colspan="4">
                    <p align="center">
                        Contribuinte
                    </p>
                </th>
            </tr>
           <tr>
                <th>
                    %
                </th>

                <th>
                    R$ Ano
                </th>
                <th>
                    Acumulado R$
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Previdencia.GroupBy(g => g.NmPessoa))
            {
                double subtotal = 0;
                foreach (var contribuicoes in item.ToList())
                {
                    subtotal += contribuicoes.Contribuinte;
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => contribuicoes.dtCompetencia)
                        </td>
                        <td>
                            11
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
                        </td>
                        <td>
                            @subtotal.ToString("c")
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>

My Model looks like this:

public class Previdencia
    {
        [Key]
        public Int64 Cod_Previdencia { 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; }

    }
    
asked by anonymous 05.03.2015 / 22:05

1 answer

1

Pretty simple. Just change your GroupBy() .

By comment was asked about the totalization on all records. This can be done by initializing a variable outside the loop and incrementing it within the loop:

        @{ var total = 0; }

        @foreach (var item in Model.Previdencia.GroupBy(g => new { g.NmPessoa, g.dtcompetencia.Year }))
        {
            double subtotal = 0;
            foreach (var contribuicoes in item.ToList())
            {
                subtotal += contribuicoes.Contribuinte;
                total += contribuicoes.Contribuinte;

                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.dtCompetencia)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.dtcompetencia)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
                    </td>
                    <td>
                        @subtotal.ToString("c")
                    </td>
                </tr>
            }
        }
    
05.03.2015 / 22:25