var orders = new [] {
new {Side = "Buy", Price = 100, Quantity = 1 },
new {Side = "Buy", Price = 101, Quantity = 10 },
new {Side = "Buy", Price = 100, Quantity = 1000 },
new {Side = "Buy", Price = 100, Quantity = 10000 },
new {Side = "Sell", Price = 100, Quantity = 10000 }
};
var orderboook
= from o in (
from order in orders
group order by order.Side into sideGroup
select new {
Side = sideGroup.Key,
SideGroup =
from s in sideGroup
group s by s.Price into g
select new {
Side = sideGroup.Key,
Price = g.Key,
Quantity = g.Sum( s => s.Quantity)
}
}
)
from g in o.SideGroup
select g;
orderboook.Dump(); // .Dump() is LINQPad helper method...