回答

收藏

Dapper多个连接的映射SQL查询

技术问答 技术问答 164 人阅读 | 0 人回复 | 2023-09-14

我想将复杂的对象映射到两个内部连接查询的简化结果中。我知道我们已经解决了映射内部连接的解决方案,但我想映射两个内部连接的结果。
8 [* Q: K$ s' e. O. h$ L这是场景:
. v: u# T- a0 ?% G6 z, y我的课程是:4 `! {- w3 x* d
public class Order    public int id { get; set;    public string order_reference { get; set;    public string order_status { get; set;    public List OrderLines { get; set; }}public class OrderLine{    public int id { get; set;    public int order_id { get; set;    public string product_number { get; set;    public List OrderLineSizes { get; set; }}public class OrderLineSize{    public int id { get; set;    public int order_line_id { get; set;    public string size_name { get; set; }}订单将OrderLines并将作为列表OrderLine作为OrderLineSizes作为列表。" h  o9 h' ]7 ?4 s! s3 t: c
现在,这是我想要的List使用正确数据填充查询的基础:! x5 V6 T  ~" `  O0 e( a
SELECT *FROM orders_mstr oINNER JOIN order_lines ol ON o.id = ol.order_idINNER JOIN order_line_size_relations ols ON ol.id = ols.order_line_id到目前为止,我已经尝试过这
, U! j* Q  r1 q1 }* |+ `: P, r, TorderDetail = o);                                                                                                      if (orderDetail.OrderLines == null)                        orderDetail.OrderLines = new List();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;orderDetail.OrderLines.Add(ol);                    return orderDetail;           .AsQueryable();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;var resultList = lookup.Values;orderDetail = o);                    }                    if (orderDetail.OrderLines == null)                        orderDetail.OrderLines = new List();                    orderDetail.OrderLines.Add(ol);                    return orderDetail;                }).AsQueryable();            var resultList = lookup.Values;我可以成功地使用这种方法OrderLine映射订单对象,但我也想填写正确的数据OrderLineSizes。4 L. f4 y% z) ?! \  f1 K$ ^( j
                                                               
* H! L" f9 D+ P    解决方案:                                                               
& p3 l& E- y' w# O* J/ b                                                                我尽力解决。* q6 J* G! h2 a6 }
这是根据我的更简单,更准确的解决方案:  o+ r2 Z# k0 v' Y
var lookup = new Dictionary<i>();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;var lookup2 = new Dictionary<i>();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;connection.Query(@&quot;                    SELECT o.*,ol.*,ols.*                    FROM orders_mstr o                    INNER JOIN order_lines ol ON o.id = ol.order_id                    INNER JOIN order_line_size_relations ols ON ol.id = ols.order_line_id                               &quot;,(o,ol,ols) =>                                                   OrderDetail orderDetail;                if (!lookup.TryGetValue(o.id,out orderDetail))                                                           lookup.Add(o.id,orderDetail = o);                                                         OrderLine orderLine;                if (!lookup2.TryGetValue(ol.id,out orderLine))                                                           lookup2.Add(ol.id,orderLine = ol);                    orderDetail.OrderLines.Add(orderLine);                                                         orderLine.OrderLineSizes.Add(ols);                return orderDetail;        ).AsQueryable();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;var resultList = lookup.Values.ToList();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则