2

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?

3 Answers 3

0

You don't need to use the where like that just get to the condition directly. I think its something like that, if you post your entities it would be a little more easier.

 var matchingEmployees = ctx.Employee
    .Include("EmployeeDepartment")
    .Include("EmployeeDepartment.DepartmentAddress")
    .Where(p=> p.DepartmentAddress.City == "New York)))
    .ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for answer. Wouldn't that only work if there were one-to-one relationships?
Try it :) It doesn't matters if its one to one or not, if it doesn't work post your entity with at least the properties you need to use.
@MattheW It will work assuming it is a many-to-one relationship where each employee has one Department, and each Department has one address. It depends on which direction you are navigating the many-to-one.
Actually it's the opposite - one employee can be assigned to many departments and one department can have assigned many addresses. Sorry if that wasn't clear in original question. :(
0

Maybe this:

ctx.Employee
    .Include( e => e.EmployeeDepartment)
    .Include( e => e.EmployeeDepartment.DepartmentAddress)
    .Where(e => && e.EmployeeDepartment.DepartmentAddress.All(y => y.City == "New York")).ToList();

1 Comment

I'm not able to test this, but if works I recommend to inspect the generated SQL to see how the Entity Framework resolve this query.
0

"What I'm trying to do is write Entity Framework query, which will return, for example, all employees which are assigned to department in City of New York."

If you're not interested in actually retrieving the departments, and only want to filter by them, then a join would be more appropriate. It's easier for other developers to see that your intention is to narrow down the list of employees, rather than simply narrowing down the list of nested department addresses. This can be accomplished in extension syntax(which I prefer), but joins are much more readable in expression syntax:

var employeesInNewYorkDepartments = 
    (from employee in ctx.Employee
    let department = employee.EmployeeDepartment
    let departmentAddress = department.DepartmentAddress
    where departmentAddress.City == "New York" 
    select employee).ToList();

Hopefully I don't have syntax errors. This assumes the relationship from employee to department is many-to-one. I.e. many employees are in a department.

If it's the opposite for both relationships:

var employeesInNewYorkDepartments = 
    (from employee in ctx.Employee
    join department in employee.EmployeeDepartments    
    join departmentAddress in department.DepartmentAddresses
    where departmentAddress.City == "New York" 
    select employee).Distinct().ToList();

Note your navigation property name should be plural when navigating in the many direction. They represent access to a collection and therefore a singular navigation property name is extremely misleading. (The entity class name would still be singular though as it declares the structure of a single item.)

2 Comments

Thank you for answer. Actually it's the opposite - one employee can be assigned to many departments and one department can have assigned many addresses. Sorry if that wasn't clear in original question.
@MattheW I've added another example that might help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.