Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq orderyby boolean

I have a Linq query which returns an ordered list. It works, but when sorting boolean values it always puts the false items first.

return from workers in db.Workers
               orderby workers.active, workers.naam 
               select workers;

Is there a way to order the true items first?

like image 202
DavidVdd Avatar asked Feb 28 '12 11:02

DavidVdd


2 Answers

Use the order by descending option and it will reverse the list. See MSDN Soring Data for more examples on sorting.

return from workers in db.Workers
           orderby workers.active descending, workers.naam 
           select workers;
like image 108
Nix Avatar answered Nov 15 '22 22:11

Nix


The OrderBy method will sort items in ascending order by default. Now, given that the numeric representation of a boolean is:

  • false = 0
  • true = 1

false values will naturally come first. If you want to reverse the order just use the descending keyword:

return from workers in db.Workers
               orderby workers.active descending, workers.naam 
               select workers;
like image 29
Enrico Campidoglio Avatar answered Nov 15 '22 22:11

Enrico Campidoglio