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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:24:28+00:00 2026-05-20T22:24:28+00:00

Can someone explain why this exception is occuring the in the following LINQ query:

  • 0

Can someone explain why this exception is occuring the in the following LINQ query:

        return (from c in dc.Classifications
                where c.Id == classificationId
                select new Classification()
                {
                    Description = c.Description,
                    ParentId = Convert.ToInt16(c.ParentId),
                }).Single<Classification>();

dc is the datacontext, Classification is a class containing the int property ParentId. The ParentId column is a nullable int from a Sql Server database. In the case of the ParentId field being null in the database, the statement returns an InvalidOperationException.

In other words, why does the above query fail and
‘int y = Convert.ToInt16(null);’
work?

  • 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-20T22:24:28+00:00Added an answer on May 20, 2026 at 10:24 pm

    If c.PartentId can be null, then Convert.ToInt16(null) would throw an exception.

    Since you indicate that Classification.ParentId is an int, is there a reason you are using
    Convert.ToInt16 to make it a short instead? Wouldn’t you want ToInt32 instead? For that matter, why convert at all? simply:

    ParentId = c.ParentId ?? 0
    

    …and just to nit-pick, technically you don’t need to specify your type at the end of that Linq expression:

    .Single<Classification>()
    

    you can omit that, since it is determined by the compiler, and just do:

    .Single()
    

    Update:

    Oh I see, I’m sorry I mis-read your original question. The question is really why does:

    int y = Convert.ToInt16(null);
    

    work, while the same thing in a Linq2Sql expression throws an exception.

    I don’t have a great answer to that, other than to point out that while the expressions look the same in code,t hey are actually handled by 2 differnet Linq implementations. (Much the same as an interface can have different backing implementations).

    In the case of:

    int y = Convert.ToInt16(null);
    

    You are making a direct call to Convert.ToInt16. This seems to convert a null into default<T> where T is the desired type (so it return 0 in this case).

    However when used in a Linq2Sql expression, the expression and its projection are handed off to Linq2Entities or Linq2Sql to process. There could just be a bug in that stuff somewhere. Used as a basic Linq2Objects (or whatever you want to call it) it actually seems to work OK:

    [TestMethod] // test passes
    public void TestLinqToObjects()
    {
      var stuff = new List<int?>() { null };
      var y = (from x in stuff
               select Convert.ToInt32(x))
               .First();
      Assert.AreEqual(0, y);
    }
    

    The above “select” works, however putting the same Linq expression against a Linq2Sql or EntityFramework collection would cause a different implementation of the Linq processor to handle the expression,a nd it might do something different to try to optemise the expression, or to turn some of it into a SQL statement, or could just have bugs that the other implementations don’t.

    I know that doesn’t really solve your issue, but it might help to explain it?

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

Sidebar

Related Questions

Just for review, can someone quickly explain what prevents this from working (on compile):
Can someone explain why this script throws an exception? $byteArray = @(1,2,3) write-Output (
Can someone please explain to me why does COMMIT in this function returns EXCEPTION
Can someone explain this result to me. The first test succeeds but the second
Can someone explain what this means? int (*data[2])[2];
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone explain to me why this code prints 14? I was just asked
I'm hoping someone can clarify this behavior for me, and explain how ASP.NET is
Can someone explain to me this StaleDataException 07-11 19:58:23.298 E/AndroidRuntime( 1044): Uncaught handler: thread
I'm revising my question from a few days ago. Rewrote my query. Can someone

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.