Algorithm for adding daughter account values in parent accounts in C # .NET

3

What happens is that I have a list with three fields, which are: Cuenta , importe_A e importe_B .

Example of information:

Cuenta = "1000.2.15.3"
Importe A = 230  
Importe B = 500  

Cuenta = "1000.2.15.4"  
Importe A = 100  
Importe B = 30  

Cuenta "1000.2.16.4"
Importe A = 160  
Importe B = 35  

What I want to do, is to bring the amount of the accounts according to their level, and the upper level must bring the sum of the daughter accounts.
For example the 1000.2.15.4 account has four levels (where the level 1 would be 1000 , the level 2 2 , the 3 15 and the 4 4 ), each level of the account is separated by .

An example of input / output data that I intend are:

Entry

  

The amount_A of account 1000.2.15.4 is 100

     

The Amount_A of account 1000.2.15.3 is 230

     

The amount_A of account 1000.2.16.4 is 160

Exit

  

the sum of the amount_A of account 1000.2.15 is 330

     

the sum of the amount_A of account 1000.2.16 is 160

     

the sum of the amount_A of account 1000.2 is 490

     

the sum of the amount_A of account 1000 is 490

Each account has the sum of the amount according to the matching parent account and so on until the last parent is reached, that is until the account no longer has "."

My question is, will there be a way to separate the accounts by "." to obtain the parents, and add the amount of each account in their respective parent?

I managed to separate the accounts with a split ('.') but I can not make the sum of their amounts in the parent accounts.

    
asked by Alfredo Josue Boche 26.10.2018 в 05:02
source

1 answer

2

You can solve your problem using Linq and GroupBy .

Assuming that your class is defined as follows:

public class C
{
    public string Cuenta { get; set; }
    public int ImporteA { get; set; }
    public int ImporteB { get; set; }

    //Método que devuelve el string correspondiente a la cuenta padre
    //También podría definirse como Propiedad, y podría ser hasta mas sencillo
    public static string GetCuentaPadre(string grupo)
    {
        var Splitted = grupo.Split(".");
        return string.Join(".",Splitted.Take(Splitted.Count() - 1).ToArray());
    }
}

Then, I define a List<C> with the data that you raised in the example

List<C> Lista = new List<C>
        {
            new C { Cuenta= "1000.2.15.3", ImporteA=230, ImporteB=500 },
            new C { Cuenta= "1000.2.15.4", ImporteA=100, ImporteB=30 },
            new C { Cuenta= "1000.2.16.4", ImporteA=160, ImporteB=35 }
        };

Then, using Linq and GroupBy I get an anonymous collection

//Uso el primer select para crear un objeto anónimo que tenga la propiedad CuentaPadre
var Result = Lista.Select(r => new
    {
    CuentaPadre = C.GetCuentaPadre(r.Cuenta),
    r.ImporteA,
    r.ImporteB
    })
        //Agrupo por CuentaPadre (que fue definida arriba)
        .GroupBy(x => x.CuentaPadre)
        //Ahora si realizo el select del objeto final
        .Select(x => new
            {
            //x.Key representa la propiedad por la cual se realizó el group by
            CuentaPadre = x.Key,
            //Utilizo Sum para generar los importes
            ImporteA = x.Sum(d => d.ImporteA),
            ImporteB = x.Sum(d => d.ImporteB)
            });

Now Result is a IEnumerable of anonymous type that has the information you intended.

foreach(var resultado in Result)
        {
            Console.WriteLine("Cuenta {0} , ImporteA {1}, ImporteB {2}",resultado.CuentaPadre,resultado.ImporteA,resultado.ImporteB);
        }

This will generate as output:

Cuenta 1000.2.15 , ImporteA 330, ImporteB 530
Cuenta 1000.2.16 , ImporteA 160, ImporteB 35

I leave you a DotNetFiddle with the example running for any questions.

Successes!

    
answered by 26.10.2018 / 06:45
source