Custom orderby in linq to sql

I have already searched for archives, but cannot find a solution that works for linq for sql.

How to create custom order in linq for sql so that it generates SQL code like this

ORDER BY
CASE SEASON
  WHEN 'WINTER' THEN 1
  WHEN 'SPRING' THEN 2
  WHEN 'SUMMER' THEN 3
  WHEN 'AUTUMN' THEN 4
END

Note that custom comparators do not seem to compile, but OrderByWeight, as seen from this tutorial ( http://www.skindog.co.uk/2009/03/18/custom-sorting-order-in-linq-order -by-weighting / ) doesn't seem to exist

Note

I want the order to be executed on the sql server, and not in C #, as this will give me different results, as I am splitting my results.

+5
source share
2 answers

Here is an approach using lambda expression


MyTable
   .OrderBy (t => (t.Season == "Winter") ? 1 : (t.Season == "Spring") ? 2 : [...])
   .Select (
      t => new
{ MyColumn = t.MyColumn ... } )
+10

, , :

( )

from s in
  (from x in y select new { Sort = x.z == 'WINTER' ? 1 : x.z == 'SPRING' ? 2 : [...], Data = x })
orderby s.Sort
select s.Data

-

,

0