How to use QueryMultiple method to allows you to get a result and a count at the same time in Dapper?

.net c# dapper sql sql-server

Question

I created two SQL command segments that I want to combine into one query as follows:

SELECT COUNT(*) FROM books

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

How do I useconn.QueryMultiple how to obtaincount AND list of books both at once?

Moreover, I don't want to develop any additional entity classes.

1
6
6/8/2016 11:44:19 AM

Accepted Answer

According to 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();
}
9
6/9/2016 3:07:40 AM


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow