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 8466595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:23:12+00:00 2026-06-10T15:23:12+00:00

With the code given from this question OrderBy is not translated into SQL when

  • 0

With the code given from this question
OrderBy is not translated into SQL when passing a selector function

Func<Table1, string> f = x => x.Name;
var t = db.Table1.OrderBy(f).ToList();

The translated SQL is:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name]
FROM [dbo].[Table1] AS [Extent1]

OK.

I can understand that the code compiles : IQueryable inherits from IEnumerable, which have an OrderBy method taking a Func<TModel, TValue> as parameter.

I can understand that the ORDER BY clause is not generated in SQL, as we didn’t pass an Expression<Func<TModel, TValue>> as the OrderBy parameter (the one for IQueryable)

But what happens behind the scene ? What happens to the “wrong” OrderBy method ? Nothing ? I can’t see how and why… Any light in my night ?

  • 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-06-10T15:23:14+00:00Added an answer on June 10, 2026 at 3:23 pm

    Because f is a delegate rather than an expression, the compiler picks the IEnumerable OrderBy extension method instead of the IQueryable one.

    This then means that all the results are fetched from the database, because the ordering is then done in memory as if it were Linq to Objects. That is, in-memory, the ordering can only be done by fetching all the records.

    Of course, in reality this still doesn’t actually happen until you start enumerating the result – which in your case you do straight away because you eager-load the result with your call to ToList().

    Update in response to your comment

    It seems that your question is as much about the IQueryable/IEnumerable duality being ‘dangerous’ from the point of view of introducing ambiguity. It really isn’t:

    t.OrderBy(r => r.Field)
    

    C# sees the lambda as an Expression<> first and foremost so if t is an IQueryable then the IQueryable extension method is selected. It’s the same as a variable of string being passed to an overloaded method with a string and object overload – the string version will be used because it’s the best representation.

    As Jeppe has pointed out, it’s actually because the immediate interface is used, before inherited interfaces

    t.AsEnumerable().OrderBy(r => r.Field)
    

    C# can’t see an IQueryable any more, so treats the lambda as a Func<A, B>, because that’s it’s next-best representation. (The equivalent of only an object method being available in my string/object analogy before.

    And then finally your example:

    Func<t, string> f = r => r.Field;
    t.OrderBy(f);
    

    There is no possible way that a developer writing this code can expect this to be treated as an expression for a lower-level component to translate to SQL, unless the developer fundamentally doesn’t understand the difference between a delegate and an expression. If that’s the case, then a little bit of reading up solves the problem.

    I don’t think it’s unreasonable to require a developer to do a little bit of reading before they embark on using a new technology; especially when, in MSDN’s defence, this particular subject is covered so well.

    I realise now that by adding this edit I’ve now nullified the comment by @IanNewson below – but I hope it provides a compelling argument that makes sense 🙂

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

Sidebar

Related Questions

In a continuation from this question . I'm trying to bind a given function
For an algorithm competition training (not homework) we were given this question from a
I'm learning about pointers. Given this code: FILE* from = fopen("in.txt", "r"); FILE* to
Given below is program for encrypting a string. I had taken this code from
Given: code inside a stored proc: select bleh into #tblTemp from FunctionThatReturnsTable('some','params') -- do
In the example enumeration code given in this question , reproduced below, why does
Follows on from this question Algorithm to generate (not quite) spanning set in Python
building upon the $.fn.serializeObject() function from this question , i'd like to be able
I found this question on Stack Overflow, and used the code given in the
On my machine Linux machine ulimit -n gives 1024 . This code: from tempfile

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.