How to check a var for null value?
var result = db.SingleOrDefault<TdUsers>(getUserQuery);
In above code SingleOrDefault will return null vale or the specified generic type(it's known on runtime).
Inorder to check whether the returned values is null or not you can simply use
if(result!=null)
{
//do your code stuff
}
else
{
//stuff do be done in case where result==null
}
if (result == null || result.Count() == 0) {
// Checks whether the entire result is null OR
// contains no resulting records.
}
I think the problem is not in your check for null
, because linq is lazy loading. Your error is in using the expression db.SingleOrDefault<TdUsers>(getUserQuery);
.
.Single<T>(expression)
does not return null - it errors if the result returns no values.
.SingleOrDefault<T>(expression)
, however, returns a null value if the expression results in no values - and therefore is best combined with an if (result == null)
type check, as you're using here.