Dapper將特定列的子集直接返回到DTO

.net c# dapper orm sql-server

Dapper可以將數據直接返回到只有字段子集的DTO / POCO中 - 即我可以使用不包含db表中所有列的類嗎?

例如,如果我有以下查詢(原諒我的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

我如何使用Dapper將其讀入以下類的數組/ IEnumerable:

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

謝謝蒂姆

一般承認的答案

你可以這樣做

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


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因