I am using PetaPoco Micro-ORM with C# 4.0.
The code below retrieves a single row from the database:
var result = db.SingleOrDefault<TdUsers>(getUserQuery);
I would like to check whether or not the result contains any rows, and whether is null. What is the best way to do this?
I think the problem is not in your check for
null, because linq is lazy loading. Your error is in using the expressiondb.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 anif (result == null)type check, as you’re using here.