Sort by month in linq and EF

后端 未结 5 2077
傲寒
傲寒 2021-01-28 17:09

I have a linq query and inside of it is the month name. I want the results sorted by the month (Jan, Feb, Mar, ...).

Currently I have the below but it\'s giving me and e

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-28 17:15

    The best way and easiest way is create a stored procedure

    Create procedure sortevents
       as
     SELECT *
            FROM [SAGMJ].[dbo].[Events]
            ORDER BY CASE 
                     WHEN MONTH([StartAt]) = MONTH(GETDATE())
                        AND YEAR([StartAt])  = YEAR(GETDATE()) THEN 0
                       ELSE 1
                     END, [StartAt];
    

    //Next call your sp

    var v = (dc.sortevents()).ToList();
    

提交回复
热议问题