LINQ query checks for null
- by user300992
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.