How can I do an orderby thenby using the above syntax without using extension syntax.
Use a comma between the fields:
orderby a, b
And what does the orderby, orderby do?
When you use orderby
twice in a row the elements conceptually will first be sorted using the first orderby
, and then sorted again using the second orderby
. Because the sorting is defined to be a stable sort (objects which are tied with the second orderby
will remain in the same order as after sorting with the first orderby
it effectively means that this:
var query = from x in l
orderby x.A
orderby x.B
select x;
is equivalent to:
var query = from x in l
orderby x.B, x.A
select x;
The result is that the orderby
terms 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:
var query = from a in dc.Orders
orderby a.Date
orderby a.CustomerID
select a;
and this was the generated SQL:
SELECT [t0].[ID], [t0].[CustomerID], [t0].[Date], [t0].[Description]
FROM [dbo].[Order] AS [t0]
ORDER BY [t0].[CustomerID], [t0].[Date]
Note that the orderby a.Date
is not ignored. Both terms are included in the ORDER BY clause, but in the opposite order than you might have intended.