2

I have a table named "Employees". The employeeId from this table MAY be in another table (a many-to-many join tbale) named "Tasks". The other field in the "Tasks" table is a taskId linked to the "TaskDetails" table. This table includes details such as budgetHours.

Using EF4, how do I write the WHERE statement such that the return is employees assigned to tasks where the budgetHours is > 120 hours?

THe WHERE statement in the following limits rows in the Employees table but now I need to add the conditions on the TaskDetails table.

var assocOrg = Employees.Where(x => x.EmployeeTypeID == 2 && x.DepartmentName == "ABC").Select (x => x.EmployeeID);

Thanks!

3 Answers 3

4

If Employees has a navigation property named Tasks, try this:

var assocOrg = Employees.Where(x => x.EmployeeTypeID == 2 && 
                                    x.DepartmentName == "ABC" &&
                                    x.Tasks.Any(t => t.BudgetHours > 120))
                        .Select (x => x.EmployeeID);
Sign up to request clarification or add additional context in comments.

Comments

1

If you table sturcture is as below,

TableName            Employee  Task               TaskDetails
ReferenceKeys        EmpID     EmpdID/TaskID      TaskID/BudgetHours

then use,

Employee.Where(x => x.Task.EmpID == x.EmpID && x.Task.TaskDetails.TaskID == x.Task.TaskID && x.Task.TaskDetails.BudgetHours > 120).select(x => x.EmpID)

Comments

0

Assuming you have a Tasks navigation property on the Employee entity, this should be straightforward:

var assocOrg = Employees.Where(x => x.Tasks.Any(t => t.BudgetHours > 120) && x.DepartmentName == "ABC").Select (x => x.EmployeeID);

Of course, this requires the Tasks property to be resolved at this point, either explicitly, via lazy-loading, or with .Include().

(kudos to @adrift for getting Tasks.Any() right... oops.)

Comments

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.