I’m using the following query syntax
from table
where
where
orderby
orderby
Where the first orderby is a date and second orderby is a date. I would assume this would work like orderby thenby but appears to be doing something else.
-
How can I do an orderby thenby using the above syntax without using extension syntax. (Got it)
-
And what does the orderby, orderby do?
Use a comma between the fields:
When you use
orderbytwice in a row the elements conceptually will first be sorted using the firstorderby, and then sorted again using the secondorderby. Because the sorting is defined to be a stable sort (objects which are tied with the secondorderbywill remain in the same order as after sorting with the firstorderbyit effectively means that this:is equivalent to:
The result is that the
orderbyterms are swapped from what you probably intended.Testing it with LINQ to SQL
This can be verified by trying it in LINQ to SQL. I created the following query:
and this was the generated SQL:
Note that the
orderby a.Dateis not ignored. Both terms are included in the ORDER BY clause, but in the opposite order than you might have intended.