Filter by group and minimum value

Asked

Viewed 80 times

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’d like to return the records IdPessoas with its lowest values of DataVen. Example:

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

I would like to do this using Linq. I am using Entity Framework.

  • 1

    you want to group by the lowest values of Dataven ? or sort by the lowest value ?

  • It is , is confused, the way it is difficult to answer, try to make clearer what you want, if possible show better examples or what you did. Enjoy learning to use the correct terms: http://answall.com/q/79894/101

  • Group by Idpessoa and Dataven.

  • How many "lowest values" do you want to return? You are using Entity Framework?

  • I can have n Idpeople and n Dataven for these ids. I would like to filter only the lowest values of Dataven by Idpessoa. I’m using Entity Framework.

1 answer

2


Well, I don’t know the name of the entity. I’ll assume it is Registro, then the DbSet is called Registros by convention.

This takes each person’s oldest due date:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.