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

时间:2012-12-13 08:39:35

标签: 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();
   ...
} 

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您可以使用查询扩展名来读取单个内容,例如

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();