我编写了两段SQL命令,想要在一个查询中处理如下:
SELECT COUNT(*) FROM books
SELECT * FROM books ORDER BY bookID OFFSET 1000 ROWS FETCH NEXT 10 ROWS ONLY
如何使用conn.QueryMultiple
方法同时获取count
和list of books
?
答案 0 :(得分:8)
来自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();
}
答案 1 :(得分:1)
为了更好的表现
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();