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

  • SEARCH
  • Home
  • 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 6735617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:02+00:00 2026-05-26T11:02:02+00:00

We are migrating some code to use Entity Framework and have a query that

  • 0

We are migrating some code to use Entity Framework and have a query that is trying to sort on a Nullable field and provides a default sort value is the value is null using the Nullable.GetValueOrDefault(T) function.

However, upon execution it returns the following error:

LINQ to Entities does not recognize the method ‘Int32 GetValueOrDefault(Int32)’ method, and this method cannot be translated into a store expression.

The query looks like:

int magicDefaultSortValue = 250;
var query = context.MyTable.OrderBy(t => t.MyNullableSortColumn
                                          .GetValueOrDefault(magicDefaultSortValue));

From this answer I can see that there is a way to provide “translations” within your EDMX. Could we write a similar translation for this coalescing function?

NOTE: When I tried, the ?? coalescing operator instead of GetValueOrDefault in the query it does work. So perhaps whatever makes that work could be leveraged?

  • 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-26T11:02:03+00:00Added an answer on May 26, 2026 at 11:02 am

    I believe you found your answer. When you use ??, EF generates SQL using a CASE to select your sort value if the value is null, and then sorts on that.

    MyTable.OrderBy (t => t.MyNullableSortColumn ?? magicDefaultSortValue).ToArray();
    

    will generate the following sql:

    -- Region Parameters
    DECLARE p__linq__0 Int = 250
    -- EndRegion
    SELECT 
    [Project1].[MyColumn1] AS [MyColumn1], 
    [Project1].[MyNullableSortColumn] AS [MyNullableSortColumn]
    FROM ( SELECT 
        CASE WHEN ([Extent1].[MyNullableSortColumn] IS NULL) THEN @p__linq__0 ELSE [Extent1].[MyNullableSortColumn] END AS [C1], 
        [Extent1].[MyColumn1] AS [MyColumn1], 
        [Extent1].[MyNullableSortColumn] AS [MyNullableSortColumn]
        FROM [dbo].[MyTable] AS [Extent1]
    )  AS [Project1]
    ORDER BY [Project1].[C1] ASC
    

    As an aside, I would recommend getting LINQPad which will let you work with your EF models and view the sql being generated. Also, it is helpful to know about the EntityFunctions class and SqlFunctions class as they provide access to several useful functions.

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

Sidebar

Related Questions

I am migrating some code and have nicely formatted Look-up Tables but they have
I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4. I
We are migrating from an ugly persistence framework (custom, that probably looked cool at
I've been migrating some of my MySQL queries to PostgreSQL to use Heroku. Most
We are gradually migrating a Spark based project to use Razor and I have
I'm migrating some code from the ASPX view engine to Razor and I've run
I'm in the process of migrating some of our old code into Java and
OK, so I have a database field of type char(1) that has a small
I use Hibernate to generate my database automatically for testing, and I have some

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.