Filter by group and minimum value

2

I have the following table:

*********************************
* IdPessoa *  DataVen  * Observ *
* 10000000 *  01/01/15 * Teste1 *
* 10000001 *  01/01/15 * Teste2 *
* 10000000 *  01/01/12 * Teste3 *
* 10000001 *  01/01/13 * Teste4 *
* 10000001 *  01/01/11 * Teste5 *
* 10000000 *  01/01/14 * Teste6 *
* 10000001 *  01/01/14 * Teste7 *
*********************************

I would like to return the IdPessoas records with their smallest values of DataVen . Example:

* 10000000 *  01/01/12 * Teste3 *
* 10000001 *  01/01/11 * Teste5 *

I would like to do this using Linq. I'm using Entity Framework.

    
asked by anonymous 10.08.2015 / 16:09

1 answer

2

Well, I do not know the name of the entity. I'll assume it's Registro , then the DbSet is called Registros by convention.

This takes the oldest due date of each person:

var registros = contexto.Registros
                .OrderBy(r => r.IdPessoa)
                .ThenBy(r => r.DataVen)
                .GroupBy(r => r.IdPessoa)
                .SelectMany(g => g.First())
                .ToList();
    
10.08.2015 / 18:39