gpt4 book ai didi

c# - MVC 3 Entity Framework 空结果集

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:25 24 4
gpt4 key购买 nike

我正在开发一个应用程序,它返回一个 excel 报告,每个员工都有多个角色,以及每个角色的预期和实际工作时间。例如,我可能被列为一个专栏的开发人员和另一个专栏的文学学士学位!它正在使用的查询返回一个空结果集。

    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;

}







}

我遗漏了大部分的文件格式和创建,它与空结果无关。结果应该是这样的 enter image description here

最佳答案

至少在以下三点中有一个对您来说是失败的:

  1. 验证数据库中的数据反射(reflect)了您正在搜索的内容。执行此操作的最快方法是通过某种探查器,它可以在转换时捕获您的 EF 查询并向您显示 SQL 语句。 linqpad是执行此操作的绝佳工具。

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

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

关于c# - MVC 3 Entity Framework 空结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com