Dapper : Reading directly nth item from the GridReader

dapper

Question

This what i found in dapper blog

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

My requirement is I want to directly read only "orders" . Can I do that ?

Accepted Answer

If you don't want the other two, you can call Read without ever iterating that sequence: no objects will be materialized:

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

Popular Answer

You can use Query extension to read single, e.g.

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

Edit:

To execute stored procedure:

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why