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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:00:59+00:00 2026-05-14T21:00:59+00:00

I have a userList, some users don’t have a name (null). If I run

  • 0

I have a userList, some users don’t have a name (null). If I run the first LINQ query, I got an error saying “object reference not set to an instance of an object” error.

var temp = (from a in userList
            where ((a.name == "john") && (a.name != null))
            select a).ToList();

However, if I switch the order by putting the checking for null in front, then it works without throwing any error:

var temp = (from a in userList
            where ((a.name != null) && (a.name == "john"))
            select a).ToList();

Why is that? If that’s pure C# code (not LINQ), I think both would be the same. I don’t have SQL profiler, I am just curious what will be the difference when they are being translated on SQL level.

  • 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-14T21:01:00+00:00Added an answer on May 14, 2026 at 9:01 pm

    In C# the && operator is short-circuiting so if the first condition returns false, the second condition is not executed at all. From MSDN:

    The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

    The || operator behaves in a similar way, except that it doesn’t evaluate its second argument if the first returns true.


    I don’t think this is the full story though. The rest of my post covers the following points:

    • You can log the SQL statements using DataContext.Log.
    • Your query shouldn’t generate an error no matter which way round you write it.
    • There are differences in behaviour between LINQ to objects and LINQ to SQL.
    • Your filtering might be executing locally instead of in the database.

    You can easily view the generated SQL in Visual Studio without needing a SQL profiler. You can hover your mouse over a LINQ to SQL query object and it will display the SQL. Or you can use the DataContext.Log to log the SQL statements, for example like this:

    TextWriter textWriter = new StringWriter();
    using (var dc = new UserDataContext())
    {
        dc.Log = textWriter;
        var userList = dc.Users;
        var temp = (from a in userList
                    where (a.Name.ToString() == "john") && (a.Name != null)
                    select a).ToList();
    }
    string log = textWriter.ToString();
    

    You can also log to a file or even to Console.Out:

    dc.Log = Console.Out;
    

    Doing this you can see that the query looks something like this, although you will likely have more columns in the select list:

    SELECT [t0].[Name]
    FROM [dbo].[User] AS [t0]
    WHERE ([t0].[Name] = @p0) AND ([t0].[Name] IS NOT NULL)
    

    Another point is that your query should not generate an error. Even if a.name is null, a == "john" should still work – it will just return false.

    Lastly, there is a difference between how C# normally works and how LINQ to SQL works. You shouldn’t get a null exception from the database. To demonstrate this I will make a small modification to your query – adding a ToString after a.Name:

    var temp = (from a in userList
                where (a.Name.ToString() == "john") && (a.Name != null)
                select a).ToList();
    

    Now this fails for Linq to Objects with a NullReferenceException, but it works with LINQ to SQL without throwing an exception. So I suspect that you have loaded all items from the database into memory and are filtering locally. In other words maybe you have something like this:

    var userList = dc.Users.ToList();
    

    instead of the following which would allow the database to do the filtering:

    var userList = dc.Users;
    

    So I suspect there is more to this question than meets the eye. Perhaps you can provide more details.

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

Sidebar

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.