-
-
Save mhart/1226067 to your computer and use it in GitHub Desktop.
multi map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cnn.QueryMultiple<Table<Order>,Table<OrderLine>,Order>( | |
@"select * from Orders | |
select * from OrderLine", (orders, lines) => | |
{ | |
var map = lines.ToDictionary(line => line.OrderId); | |
foreach(var order in orders) | |
{ | |
order.Lines = map[order.Id]; | |
yield return order; | |
} | |
}); | |
// another option, infer Table for single: | |
cnn.QueryMultiple<Table<Order,User>,OrderLine,Order>( | |
@"select * from Orders | |
left join Users u on u.Id = OwnerId | |
select * from OrderLine", (orders, lines) => | |
{ | |
var map = lines.ToDictionary(line => line.OrderId); | |
foreach(var order in orders.Map((o,u) => {o.Owner = u; return o;})) | |
{ | |
order.Lines = map[order.Id]; | |
yield return order; | |
} | |
}); | |
// Same code today | |
grid = cnn.QueryMultiple( | |
@"select * from Orders | |
left join Users u on u.Id = OwnerId | |
select * from OrderLine"); | |
var orders = grid.Read<Order,User,Order>((o,u) => {o.Owner = u; return o;}).ToList(); | |
var map = grid.Read<OrderLine>().ToDictionary(line => line.OrderId); | |
foreach(var order in orders) | |
{ | |
order.Lines = map[order.Id]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment