Created
September 18, 2011 23:43
-
-
Save SamSaffron/1225723 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[o.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[o.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]; | |
} |
If so that applies to the third example line as well
@william true ... changed it
@SamSaffron: .ToDictionary probably isn't right in any of the examples (why would we need multimap at all? :))
Can we get rid of even more boilerplate? For the multimap, I'd think all you really need to specify are the key selectors and the way to combine the results...
var results = cnn.Query<User, Order, OrderLine>
("select * from Users;select * from Order;select * from OrderLine")
.Map((user, order) => { order.User = user; return order; })
.MultiMap(order => order.Id,
line => line.OrderId,
(order, orderLines) => order.OrderLines = orderLines.ToList());
@JasonPunyon isn't he trying to avoid chaining in his revision?
@jcolebrand: He hasn't told me so I wouldn't know. I just followed a tweet here :)
@JasonPunyon me too! ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't there a bug in the second option? Shouldn't it return o instead of u?