David
David

Reputation: 25470

Linq to Sql, retrieving nested data structures with one query

I'm not sure if an answer for this already exists, as I can't figure out what mechanism in Linq is meant for this situation, or if I just need to do it manually.

Let's say I have 2 tables:

------------Employees-------------
EmployeeID  Name  -other columns-

   ---------EmployeeSkills-----------
   EmployeeID  Skill  -other columns-

So each employee can have 0 or more skills.

My goal is to draw this information into a data structure in memory, using one sql query

    class StaticEmployee
    {
        int EmployeeID;       
        string Name;
        List<string> Skills;
    }
    List<StaticEmployee> employees = (???).ToList();

Since my table relations are setup, I could just foreach the employees table (from employee in db.Employees select employee), however when I access the EmployeeSkills property, it is going to execute a seperate query for every employee record since that data wasn't returned with the first query.

Alternatively, and I what I want done so far as Sql is concerned, is run a query with a left join:

SELECT Employees.EmployeeID, Employees.Name, EmployeeSkills.Skill FROM Employees LEFT JOIN EmployeeSkills ON Employees.EmployeeID=EmployeeSkills.EmployeeID

That will get me my list, but I'll need to manually collate it into my list since an employee with 2 skills will return 2 rows. Is there a Linq operation that does this? Hypothetical example below

from employee in db.Employees
select new 
{
   EmployeeID = employee.EmployeeID, 
   Name = employee.Name,
   Skills = new List(
                     from employeeSkill in employee.EmployeeSkills 
                     select employeeSkill.skill
                    )
}

Upvotes: 2

Views: 2053

Answers (1)

eglasius
eglasius

Reputation: 36035

var options = new DataLoadOptions();
options.LoadWith< Employees>(e => e.EmployeeSkills);
context.LoadOptions = options;
//retrieve employees, they will come loaded with EmployeeSkills

If you were to use the linq query version instead, this will load them once as well:

from employee in db.Employees
select new 
{
   EmployeeID = employee.EmployeeID, 
   Name = employee.Name,
   Skills = employee.EmployeeSkills
}

Upvotes: 4

Related Questions