Sorting of lists C #

0

I have a question about the handling of lists ordered in C #

I basically have a list like the following

1/02/2018      Capital    $800
1/02/2018      Interes    $1000
1/02/2018      IVA        $160
1/03/2018      Capital    $800
1/03/2018      Interes    $1000
1/03/2018      IVA        $160

I would like to be able to return the list in the following way, ordered by dates

Fecha      capital     interes     iva
1/02/2018   $800        $1000       $160
1/03/2018   $800        $1000       $160

only using sql link

Any ideas?

Greetings and thanks

    
asked by Menoch Azriel Rice 09.03.2018 в 23:40
source

2 answers

2

The simplest way to order a list is to use linq through the method OrderBy to order in ascending order or OrderByDescending to order in a decendent manner.

For example:

miLista = miLista.OrderBy(x => x.Fecha );

or

miLista = miLista.OrderByDescending(x => x.Fecha )

Additionally you can use the Reverse method to reverse the order of your list.

miLista = miLista.OrderBy(x => x.Fecha ).Reverse();
    
answered by 10.03.2018 в 00:08
1

The solution you are looking for is the next one where I group by date.

    private void listar()
    {
        List<Datos> list = new List<Datos>();

        list.Add(new Datos() { fecha = "1/02/2018", name = "Capital", valor = "$800" });
        list.Add(new Datos() { fecha = "1/02/2018", name = "Interes", valor = "$1000" });
        list.Add(new Datos() { fecha = "1/02/2018", name = "IVA", valor = "$160" });
        list.Add(new Datos() { fecha = "1/03/2018", name = "Capital", valor = "$800" });
        list.Add(new Datos() { fecha = "1/03/2018", name = "Interes", valor = "$1000" });
        list.Add(new Datos() { fecha = "1/03/2018", name = "IVA", valor = "$160" });

        var results = from x in list
                      group new { x.name,x.valor} by   x.fecha;

    }    
    public class Datos
    {
        public string name { get; set; }
        public string fecha { get; set; }
        public string valor { get; set; }

    }
    
answered by 10.03.2018 в 00:34