LINQ query checks for null

Posted by user300992 on Stack Overflow See other posts from Stack Overflow or by user300992
Published on 2010-05-16T01:53:01Z Indexed on 2010/05/16 2:00 UTC
Read the original article Hit count: 414

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql