2

我正在开发一个应用程序,它返回一个 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;

        }







    }

我遗漏了文件的所有格式化和创建的大部分,它与空结果无关。结果应该是这样的 在此处输入图像描述

4

1 回答 1

1

它至少在以下三点之一上对您不利:

  1. 验证数据库中的数据是否反映了您正在搜索的内容。最快的方法是通过某种分析器来捕获转换后的 EF 查询并向您显示 SQL 语句。linqpad是一个很好的工具。

  2. 验证您的映射。您可能没有正确映射您的关系,因此不会产生任何数据。

  3. 验证您自己的约束。看起来您有基于权限的查找。在获取当前用户后放置一个断点,并确保它具有与您在Constants.SECURITY_ROLE_CODE_ADMIN条件中查找的权限 ( ) 相匹配的权限。

于 2013-01-23T20:06:00.787 回答