Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>How can I do an orderby thenby using the above syntax without using extension syntax.</p> </blockquote> <p>Use a comma between the fields:</p> <pre><code>orderby a, b </code></pre> <hr> <blockquote> <p>And what does the orderby, orderby do?</p> </blockquote> <p>When you use <code>orderby</code> twice in a row the elements conceptually will first be sorted using the first <code>orderby</code>, and then sorted again using the second <code>orderby</code>. Because the sorting is defined to be a <a href="https://stackoverflow.com/questions/1209935/orderby-and-orderbydescending-are-stable">stable sort</a> (objects which are tied with the second <code>orderby</code> will remain in the same order as after sorting with the first <code>orderby</code> it effectively means that this:</p> <pre><code>var query = from x in l orderby x.A orderby x.B select x; </code></pre> <p>is equivalent to:</p> <pre><code>var query = from x in l orderby x.B, x.A select x; </code></pre> <p>The result is that the <code>orderby</code> terms are swapped from what you probably intended.</p> <p><strong>Testing it with LINQ to SQL</strong></p> <p>This can be verified by trying it in LINQ to SQL. I created the following query:</p> <pre><code>var query = from a in dc.Orders orderby a.Date orderby a.CustomerID select a; </code></pre> <p>and this was the generated SQL:</p> <pre><code>SELECT [t0].[ID], [t0].[CustomerID], [t0].[Date], [t0].[Description] FROM [dbo].[Order] AS [t0] ORDER BY [t0].[CustomerID], [t0].[Date] </code></pre> <p>Note that the <code>orderby a.Date</code> is not ignored. Both terms are included in the ORDER BY clause, but in the opposite order than you might have intended.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload