Entity Framework 方法组包含不起作用
Entity Framework method group Include does not work
我在使用 EF 中的方法组加载相关实体时遇到了一些问题。
在简单查询中,加载相关实体 与 Include
一起工作正常。
例如:
var result =
Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null && myTeam.Contains(x.EmployeeID))
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
.OrderByDescending(x => x.SubmitedDate);
工作完美,加载项目和员工
但在下一个查询中没有加载项目和员工
var result2 = from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select item;
我试着做了这样的改变:select new { item, item.Employee, item.Project };
var result3 = from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select new { item, item.Employee, item.Project };
在那之后,result3[0].Employee
有一个值,与 result3[0].Project
(或该集合中的任何其他项目)相同。
问题是我不需要将 Employee 和 Project 作为此动态对象 result3 中的单独属性。
returnsresult2 方法中怎么可能有 Employee 和 Project? :)
我很少使用查询语法,而且我从来没有 运行 接触过使用 .Include(typeof(..).Name) 语法的人,所以请尝试以下操作:
var result2 = (from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select item)
.Include(i=>i.Projects)
.Include(i=>i.Employees)
.Include(i=>i.EmployeeDetails);
您可能还需要包含 using System.Data.Entity;
才能正常工作。
我在使用 EF 中的方法组加载相关实体时遇到了一些问题。
在简单查询中,加载相关实体 与 Include
一起工作正常。
例如:
var result =
Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null && myTeam.Contains(x.EmployeeID))
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
.OrderByDescending(x => x.SubmitedDate);
工作完美,加载项目和员工
但在下一个查询中没有加载项目和员工
var result2 = from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select item;
我试着做了这样的改变:select new { item, item.Employee, item.Project };
var result3 = from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
.Include(typeof(Project).Name)
.Include(typeof(Employee).Name)
.Include(typeof(EmployeeDetails).Name)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select new { item, item.Employee, item.Project };
在那之后,result3[0].Employee
有一个值,与 result3[0].Project
(或该集合中的任何其他项目)相同。
问题是我不需要将 Employee 和 Project 作为此动态对象 result3 中的单独属性。
returnsresult2 方法中怎么可能有 Employee 和 Project? :)
我很少使用查询语法,而且我从来没有 运行 接触过使用 .Include(typeof(..).Name) 语法的人,所以请尝试以下操作:
var result2 = (from item in Repository.Query<TimeState>(x => x.Accepted == 0 && x.ProjectID != null)
let projectId = (int)item.ProjectID
let isA = projectsIds.Contains(projectId) && item.Employee.EmployeeDetails.SuperiorID == id
let isB = item.Project.ManagerID == id && employeesTeam.Contains(item.EmployeeID)
where isA || isB
orderby item.SubmitedDate descending
select item)
.Include(i=>i.Projects)
.Include(i=>i.Employees)
.Include(i=>i.EmployeeDetails);
您可能还需要包含 using System.Data.Entity;
才能正常工作。