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
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();
}
For the better performance
var sql=@"SELECT *,count(*) over() as AllCount
FROM Books ORDER BY BookId OFFSET 1000 ROWS FETCH NEXT 10 ROWS ONLY"
var result = result = dbConnection.Query<Book, int, Tuple<Book, int>>(sql, Tuple.Create, splitOn:"AllCount").ToList();
var count = result.Select(c => c.Item2).FirstOrDefault();
List<Book> list = result.Select(c => c.Item1).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With