Search code examples
c#entity-frameworklinqeager-loadingmethod-group

Entity Framework method group Include does not work


I have some troubles with Loading Related Entities with a method group in EF.

In simple queries the Loading of Related Entities works ok with Include.

For example:

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);

Works perfect, and Loads the Project and the Employee

but in the next query does not load the Project and the Employee

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;

I tried to do such a change: 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 };  

After that, result3[0].Employee has a value, the same for result3[0].Project (or any other item from that collection).
The problem is that I don't need the Employee and the Project as separate properties in this dynamic object result3.

How is possible to have Employee and Project in the method which returns result2 ? :)


Solution

  • I rarely use query syntax, and I've never run into someone using .Include(typeof(..).Name) syntax so try the following:

    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);
    

    You may need to include using System.Data.Entity; as well for this to work.