0

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 ? :)

mihai
  • 2,746
  • 3
  • 35
  • 56
  • what happens when you access `Project` or `Employee` from `result2`? – default Jan 20 '16 at 17:10
  • By the way, why did you rollback marc_s's edit? IMO that edit improved the readability of our question. – default Jan 20 '16 at 17:16
  • @Default , in `result2` the `Employee` and the `Project` are NULL. – mihai Jan 20 '16 at 21:27
  • @Default, answer for your second question: unfortunately, was an edit which transformed my types `Employee` and `Project` into simple words. – mihai Jan 20 '16 at 21:29

1 Answers1

2

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.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57