如何包含特殊实体......?

时间:2013-07-21 11:28:54

标签: c# entity-framework silverlight-4.0

我希望在IsActive == true

时包含所有相关的OrganizationPosition
this.ObjectContext.Employees.Include("OrganizationPositions")

数据库结构

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

2 个答案:

答案 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();
}