2

我有以下 Linq 查询,我想根据以下条件对小时数WorkItem.Id求和:

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
                                                        });
4

1 回答 1

0

我不知道你希望它如何工作并且不能在这里编译它,但我认为你需要一个 group by 来获得每个工作项的小时总和,这就是你所追求的。

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)
        });
于 2013-11-04T17:14:59.790 回答