Dapper map multiple joins Sql Query

前端 未结 3 893
无人及你
无人及你 2021-02-01 11:14

I want to map complex object to dapper result from query which has two inner joins. I know we\'ve solution to map one inner join but I want to map two inner joins result.

<
3条回答
  •  情深已故
    2021-02-01 11:53

    I tried my best and solve it.

    Here is the more easy and accurate solution as per me.:

    var lookup = new Dictionary();
                var lookup2 = new Dictionary();
                connection.Query(@"
                        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           
                        ", (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();
    

提交回复
热议问题