Lambda expressions and nullable types
- by Mathew
I have two samples of code. One works and returns the correct result, one throws a null reference exception. What's the difference? I know there's some magic happening with capturing variables for the lambda expression but I don't understand what's going on behind the scenes here.
int? x = null;
bool isXNull = !x.HasValue;
// this works
var result = from p in data.Program
where (isXNull)
select p;
return result.Tolist();
// this doesn't
var result2 = from p in data.Program
where (!x.HasValue)
select p;
return result2.ToList();