ineztia ineztia - 5 months ago 233
SQL Question

dapper: Get result and count at same time using QueryMultiple

I wrote two segments of SQL command and want to process in one query like this:

SELECT COUNT(*) FROM books

SELECT * FROM books ORDER BY bookID OFFSET 1000 ROWS FETCH NEXT 10 ROWS ONLY


How can I use
conn.QueryMultiple
method to get
count
AND
list of books
at same time ?

btw: I don't want to create any extra entity classes

Answer

From github example:

var sql = @"SELECT COUNT(*) FROM books
            SELECT * FROM books ORDER BY bookID OFFSET 1000 ROWS FETCH NEXT 10 ROWS ONLY";

using(var multi = connection.QueryMultiple(sql))
{
    var count = multi.Read<int>().Single();
    var results = multi.Read<YourObject>().ToList();
}