I can't find the correct query to get all the data I need.
I've got three sample tables:
- Employee (Id, Name, Telephone)
- EmployeeDepartment (Id, EmployeeId, DepartmentId)
- DepartmentAddress (Id, DepartmentId, City, Street, IsActive...)
Each of this tables has one-to-many relation with next one. An Employee can be assigned to many departments and one department can have many addresses assigned.
I'm trying to write an Entity Framework query which will return, for example, all employees which are assigned to department in City of New York. My problem is that I don't want just employees. I need it to also include all properties of EmployeeDepartment and DepartmentAddress in Employee class.
What I'm trying to do is something like:
var matchingEmployees = ctx.Employee
.Include("EmployeeDepartment")
.Include("EmployeeDepartment.DepartmentAddress")
.Where(p=> p.EmployeeDepartment
.Where(x=>x.DepartmentAddress.Where(y=>y.City == "New York)))
.ToList();
As a result I get all Departments and all EmployeeDepartments, when I need only ones matching Where condition. What query will return the data I need?