Sort by month in linq and EF

后端 未结 5 2112
傲寒
傲寒 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:23

    If you dont want to create a table in your database, you can do like

    var names = DateTimeFormatInfo.CurrentInfo.MonthNames
                .Take(12).Select(s => new[] {s.Substring(0,3)});
    

    And then order against a separate list.

    orderby names.IndexOf(s.MonthName)
    

提交回复
热议问题