Value priority data ordering using LINQ

Asked

Viewed 213 times

5

I have a "status" table with the following values:

ID 1 = Ativo 
ID 2 = Novo
ID 3 = Excluido

How would you return a list of objects (that have the status object) prioritizing, coming first, those that have Status 2 (New) using LINQ?

I wonder if you have a better solution than doing with two (2) List.

2 answers

5

It would be something like that:

var lista = contexto.Entidade
                    .Where(/* Sua condição aqui */)
                    .OrderBy(e => e.Status == 2)
                    .ThenBy(e => e.Status)
                    .ToList();

4

var lista = contexto.Entidade.Where(/*Alguma condição*/) 
                             .OrderBy(e => e.Status.Id == 2) //Ordenar primeiramente pelo status com id = 2
                             .ThenBy(e => e.Status.Id) //Depois ordenar pelo id dos status em ordem crescente
                             .ToList(); //Converter para List

Browser other questions tagged

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