我正在使用 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);
}
有用。ProductPreview 类型对象的最后一个属性是 TotalCount,因为存储过程将总计数作为每一行的列返回。(第二个选项是存储过程返回两个记录集,但我不确定如何更改 dapper 以使用两个记录集)。有两个单独的查询不是一种选择。
在没有 totalCount 属性(因为它是开销)的情况下将 gridLines json 对象返回给客户端并将总计数从存储过程读取到某个变量的最佳方法是什么?将 gridLines 对象复制到没有 totalCount 属性的其他对象也是不必要的开销。