Use linq to group by array element -
i have data in following structure need group id , date. date field on array. need sum amounts each date grouped id @ top level, possible linq?
id number myarray[]
myarray follows:
[amount, date]
trying use this, wont work because second grouping on array on main item
input:
id number myarray 1 5 [{500.00,2001-03-08}, {200,2001-04-04}, {600, 2001-03-09}] 2 6 [{700.00,2001-03-08}, {300,2001-03-08}] 1 7 [{400.00,2001-03-08}, {100, 2001-04-04}, {550.00,2001-12-12}]
expected result:
id myarray 1 [{500.00+400.00,2001-03-08}, {200+100,2001-04-04}, {600, 2001-03-09}, {550.00,2001-12-12}] 2 [{700.00,2001-03-08}, {300,2001-03-08}] s in db.schedules group s new {s.empid, s.weekending} g select new { g.key.empid, g.key.weekending, g.sum(s => s.hours) };
var results = db.schedules.groupby(x => x.id) .select(g => new { .id = g.key, .data = g.selectmany(x => x.myarray) .groupby(x => x.date) .select(g2 => new { amount = g2.sum(x => x.amount), date = g2.key }) .toarray() });
Comments
Post a Comment