I'm not sure if this can be done, but here is the script.
I want to turn this sql to linq:
SELECT * FROM Department d
INNER JOIN Employee e ON e.DepartmentID = d.DepartmentID
Department - Employee from 1 to many relationships.
I created a custom object that I would like to populate.
public class DepartmentSummary
{
public Department Department { get; set; }
public List<Employee> Employees {get; set;}
}
In Linq, I came up with
var result = from d in dba.Department
join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
select new DepartmentSummary
{
Department = d,
Employees = j1.ToList()
};
I tried and it does not work. Can someone shed some light on me please? I would like to make an internal connection between the Department and the Employee. For each department in the result set, I would like to create one DepartmentSummary object that contains this department and a list of employees belonging to this department.
Does Linq provide a special solution for this, or should I iterate through the result set and manually create a list from the Management section?
Thanks,
EDIT: It seems like this works for me
var result = from d in dba.Department
join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
where j1.Count() > 0
select new DepartmentSummary
{
Department = d,
Employees = j1.ToList()
};