Skip to content
Advertisement

Linq query order by giving me issues

I have a stored procedure that I wrote in SSMS using joins, show below

When I execute it returns the values I want and orders them by sortID, which I later supply to a dropdown list in ASP.NET MVC.

I have since then wrote the stored procedure query using linq, shown below

The problem is using this linq statement, it is not putting the data in the correct order, I am trying to order the dropdown list by sizes M, L, XL, XXL, XXXL, but it is returning L, M, XL, XXL, XXXL, so I am assuming it is ordering the data alphabetically.

If you look at the regular SQL statement the beginning of the query has 2 items after the select clause, can I not do this same thing in linq.

Any help would be appreciated

Advertisement

Answer

I this case I would force the ordering after the query.

Here are a couple of ways to do it.

This ignores sorting on the database and does an in memory sort based on the order of the allPossibleSizesInCorrectOrder array.

You could also do it this way:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement