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