0

这是我在 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();
   ...
} 

我的要求是我想直接阅读 "orders" 。我可以这样做吗?

4

2 回答 2

2

如果你不想要另外两个,你可以Read在不迭代该序列的情况下调用:不会物化任何对象:

var customer = multi.Read<Customer>(); // not consumed
var returns = multi.Read<Return>(); // not consumed
var orders = multi.Read<Order>().ToList();
于 2012-12-14T07:24:32.220 回答
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();
于 2012-12-13T17:51:03.057 回答