Luís Tiago Luís Tiago - 1 month ago 14
C# Question

Order decimals in LINQ

I'm using entity framework to get data from a table and sort that data by a decimal column
How do i order that data in the correct way in linq?
i allready tried

list.OrderBy(x => x.DisplayOrder)


but this gives a wrong result

12.0
1.0
2.0
2.5
3.0


Thanks for the help

Answer

Is your DisplayOrder a string? If so it is ordering it by the alphabetic order instead if the numeric. Parse to decimal:

list.OrderBy(x => decimal.Parse(x.DisplayOrder))