[英]EF query with conditions from multiple tables
I have a table named "Employees". 我有一个名为“雇员”的表。 The employeeId from this table MAY be in another table (a many-to-many join tbale) named "Tasks".
该表中的employeeId可以在另一个名为“ Tasks”的表(多对多联接)中。 The other field in the "Tasks" table is a taskId linked to the "TaskDetails" table.
“任务”表中的另一个字段是链接到“ TaskDetails”表的taskId。 This table includes details such as budgetHours.
该表包括诸如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? 使用EF4,如何编写WHERE语句,以便将退货分配给budgetHours> 120小时的任务?
THe WHERE statement in the following limits rows in the Employees table but now I need to add the conditions on the TaskDetails table. 下面的WHERE语句限制了Employees表中的行,但是现在我需要在TaskDetails表上添加条件。
var assocOrg = Employees.Where(x => x.EmployeeTypeID == 2 && x.DepartmentName == "ABC").Select (x => x.EmployeeID);
Thanks! 谢谢!
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);
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)
Assuming you have a Tasks
navigation property on the Employee
entity, this should be straightforward: 假设您在
Employee
实体上具有Tasks
导航属性,这应该很简单:
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()
. 当然,这需要在此时解析Tasks属性,或者通过延迟加载或使用
.Include()
显式解析。
(kudos to @adrift for getting Tasks.Any() right... oops.) (对@adrift表示感谢,以使Tasks.Any()正确...糟糕。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.