Getting the Entity Platform for Active Download by Group

I know that changing the request form causes the Entity Framework to ignore include calls, but there is a way I can get it to load auxiliary properties when I select a lot and a group. In the following example, I want to notify all employees who have a task ordered for a specific period of time. Calling .ToArray () after I just hit the database once, but I do SelectMany and GroupBy in memory. Is there a way to make SelectMany and GroupBy happen on the SQL server and still include the ServiceType, Ship, and Employee data?

Iโ€™m looking for a way to make one SQL call in the database and eventually get a list of employees who have a task for a period of time and the tasks to which they are assigned.

var employeeJobs = DataContext.Jobs. Include("ServiceType"). Include("Ship"). Include("JobEmployees.Employee"). Where(j => j.Start >= now && j.Start <= finish). OrderBy(j => j.Start). ToArray(). SelectMany(j => j.JobEmployees, (j, je) => new { Job = j, Employee = je.Employee }).GroupBy(j => j.Employee); 
+7
source share
3 answers

The following should work:

 var q = from e in DataContext.Employees where e.Job.Start > .... order by e.Job.Start select new {Employee = e, Job = e.Job, Ship = e.Job.Ship, ServiceType = e.Job.ServiceType}; // No db hit yet. var l = q.GroupBy(item=>item.Employee) // no db hit yet. .ToList(); // This one causes a db hit. 
+1
source

why don't you create a view and then reference this from EF? , this also has the added benefit of a database server doing the work, rather than an application server.

0
source

Try moving Include () to the very end after your group.

-one
source

All Articles