I have a troubles with linq.
I need to get from this:
+---------+---------+-------+
| Package | WorkDay | Hours |
+---------+---------+-------+
| Pack1 | 03/01 | 8 |
| Pack1 | 03/01 | 7 |
| Pack1 | 03/02 | 6 |
| Pack2 | 03/02 | 4 |
+---------+---------+-------+
to this:
+-------+-------+-------+
| | 03/01 | 03/02 |
+-------+-------+-------+
| Pack1 | 15 | 6 |
| Pack2 | 0 | 4 |
+-------+-------+-------+
where cell value is the SUM of Hours for that Package for that WorkDay.
This code DOES work, but it produces wrong table layout:
var query = Time
.GroupBy(t => new { t.PackageId, t.WorkDay })
.Select(t => new
{
Package = t.FirstOrDefault().Package,
WorkDay = t.FirstOrDefault().WorkDay,
Hours = t.Sum(x => x.Hours)
});
Maybe there is a way to transpose query results? Im not sure how to achieve my goal in elegant way. Could somebody help me please?