Order VARCHAR Field Custom Order in Entity Framework

I have a table field called "ApDay VARCHAR (15)" that contains "Days of the week." I want to sort the table values ​​according to the ApDay field. This means that I want to sort the results according to the days of the week. But I want to sort them by custom. This means that "Saturday" must come first, and then "Sunday, Monday, ..." and so on. I use an entity structure. Can someone tell me how to achieve this using the framework entity and C #.

Thanks in advance.

+4
source share
1 answer

Define an enumeration as shown below

public enum Day
{
    Sunday =0, Monday = 1, Tuesday = 2, Wednesday =3, Thursday = 4, Friday = 5, Saturday = 6
}

Then you can prepare a list of orders by AppDay, as shown below.

var orderedList = myTableData.OrderByDescending(x => (int)Enum.Parse(typeof(Day), x.AppDay, true)); 
+4
source

All Articles