2

Scenario:

Employee:     EmployeeId   (PK)    DepartmentId_FK  (FK)   Name and other fields
Department:   DepartmentId (PK)    CompanyId_FK     (FK)   Name and other fields
Company:      CompanyId    (PK)                            Name and other fields

How can I build a linq query that can join the three tables and returns EmployeeName, DepartmentName and CompanyName?

the following syntax does not work because there is no Navigation Property names Company on Employee.cs.

var model = DataContext.Employee
                .Include("Employer")
                .Include("Company");

What is the correct way to do this?

2

1 Answer 1

3

Try this :

 var data = DataContext.Employees.Include("Department").Include("Department.Company");

But use this with caution as eager loading can hurt back if there is large amount of data.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't need the Include("Department"); that's implied by the Include("Department.Company"). See the remarks at: msdn.microsoft.com/en-us/library/bb738708.aspx

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.