我正在开发一个应用程序,它返回一个 Excel 报告,每个员工都有多个角色以及每个角色的预期和实际工作时间。例如,我可能被列为一个专栏的开发人员和另一个专栏的 BA!。它正在使用的查询返回一个空结果集。
public ActionResult ExpectedVsActual()
{
try
{
ProjectTotalsReportViewModel model = new ProjectTotalsReportViewModel();
Employee currentUser = DataHelper.GetEmployee(User, db);
model.AvailableEmployees = db.Employees.OrderBy(e => e.LastName).ThenBy(e => e.FirstName).ToList();
if (currentUser.SecurityRoleCode == Constants.SECURITY_ROLE_CODE_ADMIN)
{
model.AvailableProjects = (db.Projects.Any() ? db.Projects.Where(p => p.ProjectCategoryCode == Constants.PROJECT_CATEGORY_DIRECT).OrderBy(p => p.ProjectName).Distinct().ToList() : new List<Project>());
model.ProjectRate = (db.EmployeeProjectRates.Any() ? db.EmployeeProjectRates.Where(epr => epr.EmployeeID == epr.EmployeeID).OrderBy(epr => epr.EmployeeID).Distinct().ToList(): new List<EmployeeProjectRate>());
}
else
{
model.AvailableProjects = (db.EmployeeProjectRates.Any(epr => epr.EmployeeID == currentUser.EmployeeID && epr.ProjectRoleCode == Constants.PROJECT_ROLE_PROJECT_MANAGER) ? db.EmployeeProjectRates.Where(epr => epr.EmployeeID == currentUser.EmployeeID && epr.ProjectRoleCode == Constants.PROJECT_ROLE_PROJECT_MANAGER).Select(epr => epr.Project).OrderBy(p => p.ProjectName).Distinct().ToList() : new List<Project>());
}
model.SelectedEmployeesForCheckBox = model.AvailableEmployees;
model.SelectedProjectsForCheckBox = model.AvailableProjects;
ViewBag.InitialLoad = true;
return PartialView("_ExpectedVsActual", model);
}
catch
{
return null;
}
}
[HttpPost]
public ActionResult ExpectedVsActual(ProjectTotalsReportViewModel Model)
{
try
{
if (ModelState.IsValid)
{
ViewBag.InitialLoad = false;
DataHelper.TimeFrame selectedTimeFrame = DataHelper.TimeFrame.Weekly;//Need to remove after adding selection for time frame
SMCContext db = new SMCContext();
ViewBag.TimeFrameSelectList = DataHelper.GetTimeFrameSelectList();
List<ProjectTimeFrame> timeFrames = new List<ProjectTimeFrame>();
ProjectViewModel Project = new ProjectViewModel();
timeFrames = DataHelper.GetProjectTimeFrames(Model.StartDate.Value, Model.EndDate.Value, selectedTimeFrame);
Project.EmployeeProjectRates = Project.EmployeeProjectRates.OrderBy(epr => epr.Employees.Single(e => e.Value == epr.EmployeeID.ToString())).ToList();
resultFile = currentPackage.GetAsByteArray();
return File(resultFile, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Expected Vs Actual.xlsx");
}
#endregion
}
else
{
return PartialView("_ExpectedVsActual", Model);
}
}
catch (Exception e)
{
throw e;
}
}
我遗漏了文件的所有格式化和创建的大部分,它与空结果无关。结果应该是这样的