Dapper:直接从GridReader中读取第n个项目

dapper

这是我在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 returns = multi.Read<Return>().ToList();
   var orders = multi.Read<Order>().ToList();
   ...
} 

我的要求是我想直接阅读“订单”。我能这样做吗?

一般承认的答案

如果您不想要其他两个,则可以在不迭代该序列的情况下调用Read :不会实现任何对象:

var customer = multi.Read<Customer>(); // not consumed
var returns = multi.Read<Return>(); // not consumed
var orders = multi.Read<Order>().ToList();

热门答案

您可以使用Query扩展来读取单个,例如

const string sql = "select * from Orders where CustomerId = @id";
var orders = connection.Query<Order>(sql, new {id = 1}).ToList(); 

编辑:

要执行存储过程:

var orders = connection.Query<Order>("GetOrders", new {id = 1}, commandType: CommandType.StoredProcedure).ToList();


许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因