Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:19:18+00:00 2026-05-31T11:19:18+00:00

The following basic LINQ to SQL statement does not result in the orderby working.

  • 0

The following basic LINQ to SQL statement does not result in the orderby working. As you can see in the T-SQL there is no orderby. Do you know why?

LINQ to SQL:

      var results = (from stats in db.t_harvest_statistics
                       orderby stats.unit_number
                       select stats.unit_number).Distinct().ToList();

Above Results in following TSQL

SELECT 
[Distinct1].[unit_number] AS [unit_number]
FROM ( SELECT DISTINCT 
[Extent1].[unit_number] AS [unit_number]
FROM [dbo].[t_harvest_statistics] AS [Extent1]
     )  AS [Distinct1]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:19:20+00:00Added an answer on May 31, 2026 at 11:19 am

    That is a limitation of SQL and Relational Algebra of where the ORDER BY is in relation to the DISTINCT.

    The ORDER BY must be “further out” in the SQL (at the “top level”) since it’s a view operation. While one can write SQL that has the ORDER BY “further in”, in relationship to a RA operation, it often results in Undefined Behavior (that sometimes works). In this light it makes sense that Linq2Sql is free to ignore the ORDER BY although, perhaps an exception would be better… it would be less subtle anyway 😉 (Actually, this same issue exists for any Linq provider that does not provide a “stricter” definition of Distinct.)

    Remove the Distinct() and the Linq2Sql should once again generate the ORDER BY as expected. The solution is just to switch the order of operations so the ORDER BY is once again at the “top level”.

    This is covered by the article Use of Distinct and OrderBy in LINQ:

    This behavior might appear strange. The problem is that the Distinct operator does not grant that it will maintain the original order of values. Applied to LINQ to SQL, this mean that a sort constraint can be ignored in the case of a query like queryA.

    The solution is pretty s[i]mple: put the OrderBy operator after the Distinct one, like in the following queryB definition:

    var queryB = 
        (from o in db.Orders
         select o.Employee.LastName)
        .Distinct().OrderBy( n => n );
    

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic LINQ Query that looks like the following: var results =
I know Linq-to-SQL is dead, but anyway, I think this is really basic, I'm
Can someone tell me the difference the following two LINQ statements please? var ChkUnique
Say I have the following basic if-statement: if (A ~= 0) % do something
The following code is visual basic, .NET, ASP... all of the above? Can anybody
I have the following basic (and maybe stupid) understanding problem in JSF: There is
Is there a simple way to remove duplicates in the following basic query: email_list
Given the following basic grammar I want to understand how I can handle comment
I have the following fairly basic greasemonkey script: var newloc = location.href.replace(/^(.*)-xyz-(.*)$/i, $1$2); if
currently i have jdbc code with the following basic stucture: get Connection (do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.