Dapper returning a sub set of specific columns directly into DTO

守給你的承諾、 提交于 2019-12-11 01:59:03

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/18950919/dapper-returning-a-sub-set-of-specific-columns-directly-into-dto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!