List records with the most recent date

1

I have the following

List<Pago> listaPago = dbc.pagos
    .GroupBy(c => c.codigoUsuario)
    .SelectMany(w => w)
    .OrderByDescending(f => f.fechaPago)
    .ToList();

And it shows me the following

codigoPago    codigoUsuario    fechaPago
01              Us-01          24/12/2017
02              Us-01          20/11/2017
03              Us-02          22/12/2017

I want the result to show me only this

01              Us-01          24/12/2017
03              Us-02          22/12/2017

... with the most current payment date for users.

    
asked by Paul Alvares 24.11.2017 в 21:53
source

2 answers

0

The idea of GroupBy is correct, but the SelectMany that follows basically cancels the effects of GroupBy and causes it to return all records.

What you need after the GroupBy is a Select (with the correct conditions) for each grouping to return a single object Pago :

List<Pago> listaPago = dbc.pagos
    .GroupBy(p => p.codigoUsuario)
    .Select(g => g.OrderByDescending(p => p.fechaPago).First()) // esta es la parte importante
    .OrderByDescending(p => p.fechaPago) // aquí puedes ordenar el resultado por lo que quieras
    .ToList();
    
answered by 24.11.2017 / 22:30
source
0

Is that you are ordering by code. GroupBy (c = > c.codigoUsuario)

should be by date. GroupBy (c => c.date)

    
answered by 30.11.2017 в 16:52