Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-to-sql orderby thenby

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.

  1. How can I do an orderby thenby using the above syntax without using extension syntax. (Got it)

  2. And what does the orderby, orderby do?

like image 281
Curtis White Avatar asked May 25 '10 19:05

Curtis White


People also ask

What is difference between OrderBy and ThenBy in LINQ?

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.

What is the use of ThenBy in LINQ?

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.

How do I use ascending order in LINQ?

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.

How do I order by two columns in LINQ?

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.


2 Answers

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.

like image 75
Mark Byers Avatar answered Sep 18 '22 14:09

Mark Byers


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.

like image 31
hemp Avatar answered Sep 18 '22 14:09

hemp