使用 dapper 进行分页的 TotalCount

Sim*_*mon 4 c# dapper webapi2

我使用 dapper 将存储过程中的结果集获取到对象列表中,并将其作为 json 返回给客户端:

public IHttpActionResult Test()
    {
        List<ProductPreview> gridLines;
        var cs = ConfigurationManager.ConnectionStrings["eordConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(cs))
        {
            gridLines = conn.Query<ProductPreview>("dbo.myStoredProcedure", new { userID = 1 },
             commandType: CommandType.StoredProcedure).ToList();
        }
        var totalCount = gridLines[0].MaxCount;//I need to know total count
        ....
        return Ok(gridLines);
    }
Run Code Online (Sandbox Code Playgroud)

有用。ProductPreview 类型的对象的最后一个属性是 TotalCount,因为存储过程将总计数作为每行的列返回。(第二个选项是存储过程返回两个记录集,但我不确定如何更改 dapper 以使用两个记录集)。不能选择进行两个单独的查询。

在没有 TotalCount 属性的情况下将 gridLines json 对象返回给客户端(因为它是开销)并将总计数从存储过程读取到某个变量的最佳方法是什么?将 gridLines 对象复制到没有 TotalCount 属性的其他对象也会产生不必要的开销。

Voi*_*Ray 5

Dapper允许您在单个查询中处理多个结果网格。

例子:

var sql = 
@"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
} 
Run Code Online (Sandbox Code Playgroud)