The Huff
The Huff

Reputation: 485

Dapper returning a sub set of specific columns directly into DTO

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

Upvotes: 2

Views: 4398

Answers (1)

Damith
Damith

Reputation: 63105

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

Upvotes: 3

Related Questions