在实体框架

时间:2015-12-08 09:47:59

标签: c# entity-framework linq iqueryable

我有一个部门对象,该对象包含员工列表。如何获得特定员工所在的部门?我有一个通用FindAll

IQueryable<Department> FindAll(params Expression<Func<Department, object>>[] includeProperties)

然后我尝试了

FindAll().Where(x => x.Employee.Any(y => y.Name == name)).FirstOrDefault();

1 个答案:

答案 0 :(得分:2)

您想加载相关实体吗?

你可以这样做:

    var employee = context.Employee.FirstOrDefault(y=>y.Name==name);
    if(employee!=null)
    {
      context.Entry(employee).Reference("Department").Load();
      var deparment = employee.Department;
    }