我希望在IsActive == true
时包含所有相关的OrganizationPositionthis.ObjectContext.Employees.Include("OrganizationPositions")
数据库结构
Employee | OrganizationPositions
--------- -------------------------
ID ID , EmployeeID , IsActive
答案 0 :(得分:1)
Include将加载指定关系的所有实体。根据条件过滤掉一些实体是不可能的。
您可以通过手动加入来过滤掉实体:
from e in ObjectContext.Employees
join op in ObjectContext.OrganizationPositions
on e.EmployeeID equals op.EmployeeID into g
select new {
Employee = e,
ActivePositions = g.Where(x => x.IsActive)
}
答案 1 :(得分:0)
不可能。但是,如果您正确声明导航属性,并且确实需要您告诉的情况,则可以使用以下代码:
//...
// First, retrieve all Employees with navigation property
var emps = new ObjectContext.Employees.Include("OrganizationPositions").AsQueryable();
// Then, clear OrganizationPositions of those that aren't active:
foreach (Employee e in emps) {
if (e.IsActive != true)
e.OrganizationPositions.Clear();
}