-1
Public Function TotalHoursWorked() As IEnumerable(Of Reporting.EmployeeViewModel) Implements IReportRepository.TotalHoursWorked**

    Dim qryEmployee = From emp In db.Employee _
    .Select(Function(emp) New With {emp.EmployeeId, emp.EmployeeName}).AsEnumerable

    Dim qryHoursWorked = From hw In db.HoursWorked
                             Select New With {hw.EmployeeId, hw.HoursWorkedStart, hw.HoursWorkedEnd}

    Return (From emp In qryEmployee _
            Join hw In qryHoursWorked On emp.EmployeeId Equals hw.EmployeeId _
            Select New Reporting.EmployeeViewModel With {
                 .EmployeeName = emp.EmployeeName,
                 .HoursWorkedStart = RoundClockInTime(hw.HoursWorkedStart),
                 .HoursWorkedEnd = If(hw.HoursWorkedEnd.HasValue, RoundClockOutTime(hw.HoursWorkedEnd), hw.HoursWorkedEnd),
                 .HoursWorked = (.HoursWorkedStart - .HoursWorkedEnd),
                 .TotalHours = Sum(.HoursWorked)
             })

End Function

我得到的错误是"Sum is either not declared or not in the current scope". 我需要在按employeeId 分组的报表页脚中显示总小时数。

关于这个的任何建议。我可能会出错。提前致谢

4

1 回答 1

0

对,VB.Net 不是我的语言(常规 c#),所以如果有任何错误,请原谅我。

似乎首先你有一个奇怪的查询语法和流畅的语法组合,你需要小心。另外AsEnumerable我认为没有必要。

拥有多个 EmployeeViewModels 也没有意义,每个 EmployeeViewModels 都有单独的工作时间,而且总数也会由其他 ViewModels 形成。你会如何展示它?以后应该为报告计算总计,或者您需要更改 ViewModel 以具有工作时间列表和总计。此外,您不应在此处设置 HoursWorked,它应该是返回计算值的只读属性。

我对您的第一个建议是加入群组。如果不进行分组或使用聚合,Sum 将不可用。

所以我的解决方案如下:

Dim Employees = From Employee In db.Employee
                Select New With 
                {
                    .EmployeeId = Employee.EmployeeId,
                    .Name = Employee.EmployeeName
                }

Dim WorkingHours = From HoursWorked In db.HoursWorked
                  Select New With 
                  {
                      .EmployeeId = HoursWorked.EmployeeId,
                      .Start = HoursWorked.HoursWorkedStart
                      .End = HoursWorked.HoursWorkedEnd
                  }

Dim EmployeeWorkingHours = From Employee In Employees
                          Group Join HoursWorked In WorkingHours
                          On Employee.EmployeeId Equals HoursWorked.EmployeeId
                          Into EmployeeHoursWorked = Group, _
                              TotalHours = Sum(HoursWorked.Start - HoursWorked.End)
                          From EmployeeHours in EmployeeHoursWorked
                          Select New Reporting.EmployeeViewModel With
                          {
                              .EmployeeName = Employee.Name
                              .HoursWorkedStart = EmployeeHours.Start
                              .HoursWorkedEnd = EmployeeHours.End
                              .HoursWorked = (EmployeeHours.Start - EmployeeHours.End)
                              .TotalHours = CType(TotalHours, Integer?) _
                                  .GetValueOrDefault(0)
                          }

现在,如果您按照上述方法创建一个新类,例如 WorkingHours,其属性为 Start、End,以及一个返回 (Start - End) 的只读属性 HoursWorked。

然后,您可以在 EmployeeViewModel 上拥有一个 Total 属性,如果您想将 HoursWorked 相加以给出您的 Total,它将使用 LINQ。

更像:

Dim WorkingHours = From HoursWorked In db.HoursWorked
                  Select New WorkingHours With 
                  {
                      .EmployeeId = HoursWorked.EmployeeId,
                      .Start = HoursWorked.HoursWorkedStart
                      .End = HoursWorked.HoursWorkedEnd
                  }

Dim EmployeeWorkingHours = From Employee In Employees
                          Group Join HoursWorked In WorkingHours
                          On Employee.EmployeeId Equals HoursWorked.EmployeeId
                          Into EmployeeHoursWorked = Group
                          Select New Reporting.EmployeeViewModel With
                          {
                              .EmployeeName = Employee.Name
                              ' This is an Enumerable<WorkingHours>
                              ' which you can use in the ViewModel
                              ' to calculate the total.
                              .WorkingHours = EmployeeHoursWorked
                          }
于 2013-07-19T20:26:29.373 回答