Reputation: 397
I have linq statement that returns value. The LINQ statement below will returns the systemUser Id and totalHours of a systemUser as shown below. However, what I really what is to get the Id and the total hours per system user. How to add group then in linq?
var th1 = (from ss in db.SystemUsers
join t in db.Timesheets on ss.Id equals t.SystemUser
where (t.Project == projectId && t.StartTime >= year_start && t.StartTime < year_end)
select new
{
ss.Id,
t.TotalHours
});
Id | TotalHours
1 | 10
1 | 20
2 | 10
2 | 5
and so on...
EXPECTED OUTPUT
Id | TotalHours
1 | 30
2 | 15
and so on...
Upvotes: 0
Views: 69
Reputation: 27461
You have to use GroupBy
var th1 =
from ss in db.SystemUsers
join t in db.Timesheets on ss.Id equals t.SystemUser
where (t.Project == projectId && t.StartTime >= year_start && t.StartTime < year_end)
group t by new { ss.Id } into g
select new
{
g.Key.Id,
TotalHours = g.Sum(x => x.TotalHours)
};
Upvotes: 1