1

当 IsActive == true 时,我想包括所有相关的 OrganizationPositions

this.ObjectContext.Employees.Include("OrganizationPositions")

数据库结构

Employee |       OrganizationPositions 
---------       -------------------------
ID              ID , EmployeeID , IsActive
4

2 回答 2

1

包含将加载指定关系的所有实体。无法根据条件过滤掉某些实体。

您可以通过手动加入来过滤掉实体:

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)
}
于 2013-07-21T11:49:54.897 回答
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();
}
于 2013-07-22T02:21:53.430 回答