2013-09-23 40 views
1

Dapper可以直接將數據返回到只包含字段子集的DTO/POCO中 - 也就是說,它可以使用不包含db表中所有列的類嗎?Dapper將特定列的子集直接返回到DTO

例如,如果我有以下查詢(請原諒我的SQL - 不是我的強項):

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 

如何使用小巧精緻的閱讀到一個數組該/下面的類的IEnumerable的:

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

感謝 添

回答

2

你可以做如下

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

輝煌。感謝Damith! 3小時進入Dapper並喜歡它。這將節省我創建數據服務和演示應用程序的時間! –

+0

btw,'選擇c.Name作爲[Customer],o.Number作爲[Order],ol.Number作爲[Line]'等避免了雙引號轉義的痛苦,技術上更加正確 –