Dapper returning a sub set of specific columns directly into DTO

.net c# dapper orm sql-server

Question

Can Dapper return data directly into a DTO/POCO that only has a subset of fields - ie can I it use classes that do not contain all the columns in the db tables?

Eg if I have the following query (excuse my sql - not my strong point):

select c.Name as "Customer", o.Number as "OrderNo", ol.Number as "Line", p.Description     as "Product", ol.Qty 
from order o
join customer c on c.Id = o.CustomerId
join orderLine ol on ol.OrderID = o.Id
join product p on p.Id = ol.ProductId
where o.date >= 1/9/2013 and o.date <= 30/9/2013

How can I use Dapper to read this into an array/IEnumerable of the following class:

class CustOrders{
  string Customer {get;set;}
  integer Order {get;set;}
  string Line {get;set;}
  string Product {get;set;}
  integer Qty {get; set;} 
}

Thanks Tim

Accepted Answer

you can do as below

    var sql  = @"select c.Name as [Customer], o.Number as [Order], ol.Number as [Line], p.Description as [Product], ol.Qty ...";
    var result = connection.Query<CustOrders>(query);



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