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?
Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.
Use ThenBy() method after OrderBy to sort the collection on another field in ascending order. Linq will first sort the collection based on primary field which is specified by OrderBy method and then sort the resulted collection in ascending order again based on secondary field specified by ThenBy method.
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.
Always make use of ThenBy() after OrderBy() because OrderBy() returns an IOrderedEnumerable which then exposes the methods ThenBy() and ThenByDescending() . This means that we can OrderBy on multiple fields by chaining OrderBy() and ThenBy() together.
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.
Performing a ThenBy in Query Expression Syntax is straighforward, simply follow the initial orderby with a comma and a 2nd statement:
// Create the data source. List<Student> students = GetStudents(); // Create the query. IEnumerable<Student> sortedStudents = from student in students orderby student.Last ascending, student.First ascending select student;
Applying a 2nd orderby using the standard query operators (extension methods) will actually apply the second orderby to the result of the query result which includes the first orderby. In effect, only the second orderby will apply, although you'll still spend CPU time calculating the first order.
This is actually answered directly in the MSDN documentation for the Enumerable.OrderBy and Enumerable.ThenBy methods.
Because IOrderedEnumerable inherits from IEnumerable, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With