Search code examples
c#dapper

Dapper Query Multiple Blank Result Set


I need to be able to tell when a query multiple async in dapper returns back a blank data set (so no rows are returned), but I am not sure of the correct way of doing this.

The following is similar to my current code:

var data= await db.QueryMultipleAsync(getDataProcedure, commandType: CommandType.StoredProcedure, param: parameters);

var table1Data = data.Read<table1Type>().First();
var table2Data = data.Read<table2Type>().First();

What I want to be able to do is to place an if around the table1Data and table2Data variables such that they are only ever populated if there is data to populate them.

Please let me know if I need to explain any further.


Solution

  • Dapper returns IEnumerable<T> collection and if no rows were read from database, the collection is empty. So you can simply use FirstOrDefault instead of First. It will result in null in case of no rows returned. (Better said it will be default for your data type table1Type which is null for all reference types.)