Reputation: 2102
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.
Upvotes: 1
Views: 3112
Reputation: 6292
You should use Any
. It will check whether the IEnumerable<T>
has any data.
var data= await db.QueryMultipleAsync(getDataProcedure, commandType: CommandType.StoredProcedure, param: parameters);
if (data.Read<table1Type>().Any() && data.Read<table2Type>().Any())
{
... // Do stuf with data
}
In my opinion it shows the intent clearer than FirstOrDefault()!=null
.
Upvotes: 1
Reputation: 5980
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.)
Upvotes: 4