Add table values

3

I have a table where I return the amounts paid per taxpayer monthly. But I need to create a subtotal field, adding up the values for each month. Much like the Fibonacci Sequence. The problem is that I need to look up to when the taxpayer made the payment. In this part I'm getting involved.

The data I currently have:

HowdoIneedthedatatoreturn:

I tried something like:

 double a = 0;
        double AnoContribuinte = 0;

        for (int i = 0; i < 10; i++)
        {
            a = a + Contribuinte;
            i++;
            Console.WriteLine(a);
        }

I just limit the i< 10 , and in fact I need to be equal to the amount of data the table has.

But in this way it goes into an loop infinite, since I will always have some value.

    
asked by anonymous 05.03.2015 / 14:20

3 answers

6

I see some problems in this code:

  • The code seems to be manipulated monetary values and using type double , this does not work, you will have rounding problems.
  • You are incrementing i twice, I can not imagine why this is necessary.
  • The data being fetched does not vary.
  • What you can do to help is this:

    var listaContribuintes = PegaContribuintes(); // este método traz os contribuintes de algum lugar
    Decimal subTotal = 0;
    
    foreach (var item in listaContribuintes) {
        subTotal += item.Contribuinte;
        item.SubTotal = subTotal;
    }
    

    I took advantage of the list that TobyMosque created in his response to set up a functional example in dotNetFiddle . You have to adapt it to your list, either as it is composed.

    If you have more details I'll update the answer.

        
    05.03.2015 / 14:36
    3

    So let's assume the following model:

    public class Pessoa
    {
        public string Nome { get; set; }
        public double Contribuinte { get; set; }
        public double SubTotal { get; set; }
    }
    

    And we have the following list, in the example below it is being loaded statically:

    var pessoas = new List<Pessoa>();
    pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
    pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
    pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
    pessoas.Add(new Pessoa { Nome = "Elias Vieira", Contribuinte = 31.87d });
    

    You can browse as follows:

    var subTotal = 0.0d;
    foreach(var pessoa in pessoas)
    {
        subTotal += pessoa.Contribuinte;
        pessoa.SubTotal = subTotal;
    }
    
        
    05.03.2015 / 14:36
    0

    As a suggestion, I would let the database solve the problem, look at this article, very good, in English!

    link

    I hope it helps!

        
    05.03.2015 / 14:52