IQueryable条件包括

问题描述 投票:1回答:1

我有一些课程:

public Department {
  public HashSet<Employee> Managers {get; set;}
  public HashSet<Employee> Workers {get; set;}
  public HashSet<Asset> Assets {get; set;}
}

我使用IQueryable获取部门集合:

IQueryable<Department> deparments = Context.Department.Where (
dept => dept.CompanyId = id)
.include (dept.Managers.Where (emp => emp.level == Position.Manager) as Managers)
.include (dept.Workers.Where (emp => emp.level == Position.Worker) as Workers)
.include (dept.Asset)

执行查询时出错。这样做的正确方法是什么?这是错误消息:

“Include属性lambda表达式'dept => {来自dept.Employee中的Employee emp,其中([emp] .Position == Position.Manager)select [emp]}'无效。表达式应表示属性访问:'t => t.MyProperty'。要定位在派生类型上声明的导航,请指定目标类型的显式类型lambda参数,例如'(Derived d)=> d.MyProperty'。

有关包含相关数据的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkID=746393。“

c# entity-framework linq iqueryable
1个回答
1
投票

由于你的ManagersWorkers属性是Department类的直接孩子,你没有在Department类中引用Employee所以你不能使用Include()用于此目的。

我认为你应该通过使用子查询来做到这一点。

var query = (from d in Contex.Department
             where d.CompanyId == id
             select new Department{
                 Managers = d.Managers.where(m => m.level == Position.Manager),
                 Workers = d.Workers.where(w => w.level == Position.Worker),
                 Asset = d.Assets,
             });

希望它能解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.