Groupby,与Linq lambda中的多个连接相加

时间:2013-11-04 16:59:01

标签: c# sql linq join

我有以下Linq查询,我想根据WorkItem.Id总结 Hours

EntryCategories
           .Join(TimeReportEntries,
                 ec => ec.Id,
                 tr => tr.WorkItem.Id,
                 (ec, tr) => new { ec, tr })
                            .Join(Timesheets,
                                  ecs => ecs.tr.Timesheet.Id,
                                  t => t.Id,
                                  (ecs, t) => new { ecs, t })
                                              .Join(Users,
                                                    ts => ts.t.User.Id,
                                                    u => u.Id,
                                                    (ts, u) => new
                                                        {
                                                            Employee = ts.t.User.FirstName + " " + ts.t.User.LastName,
                                                            Hours = ts.ecs.tr.Hours
                                                        });

1 个答案:

答案 0 :(得分:0)

我不知道你究竟想要它如何工作并且无法在这里编译它,但我认为你需要一个小组来获得每个工作项的小时数,这就是你所追求的。 / p>

EntryCategories
       .Join(TimeReportEntries, ec => ec.Id, tr => tr.WorkItem.Id, (ec, tr) => new { ec, tr })
       .Join(Timesheets, ecs => ecs.tr.Timesheet.Id, t => t.Id, (ecs, t) => new { ecs, t })
       .Join(Users, ts => ts.t.User.Id, u => u.Id, (ts, u) => new
        {
            WorkItemId = ts.ecs.tr.WorkItem.Id
            Employee = ts.t.User.FirstName + " " + ts.t.User.LastName,
            Hours = ts.ecs.tr.Hours
        })
       .GroupBy(x => x.WorkItemId)
       .Select(x => new 
        { 
            WorkItemId = x.Key, 
            Hours = x.Sum(y => y.Hours)
        });