gpt4 book ai didi

c# - Dapper 将特定列的子集直接返回到 DTO

转载 作者:行者123 更新时间:2023-11-30 22:13:36 25 4
gpt4 key购买 nike

Dapper 能否将数据直接返回到只有字段子集的 DTO/POCO 中 - 即我能否使用不包含数据库表中所有列的类?

例如,如果我有以下查询(请原谅我的 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);

关于c# - Dapper 将特定列的子集直接返回到 DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com