How to check if record exists before reading in Dapper ORM

dapper

Question

Can anyone please suggest how to check if there are any records in the resultset using Dapper ORM.

Customer objCustomer = null;
using (SqlMapper.GridReader multiResult = new DapperRepository().QueryMultiple(sql, new { id = id }))
{
    objCustomer = multiResult.Read<Customer>().Single(); //null exception
}

Accepted Answer

Yes, write this:

objCustomer = multiResult.Read<Customer>().SingleOrDefault(); //return null if not exists without error

then you can check:

objCustomer != null

Hope this helps.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why