Sorting data with value priority using LINQ

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 (which have the status object) prioritizing, coming first, those that have Status 2 (New) using LINQ?

I would like to know if you have a better solution than two (2) List.

    
asked by anonymous 16.09.2015 / 18:39

2 answers

5

It would look something like this:

var lista = contexto.Entidade
                    .Where(/* Sua condição aqui */)
                    .OrderBy(e => e.Status == 2)
                    .ThenBy(e => e.Status)
                    .ToList();
    
16.09.2015 / 18:41
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
    
16.09.2015 / 18:48