如何在实体框架方法语法中应用此查询
var company = context.companies
.Where(com => com.Departments
.Contains(context.departments
.Where(dep => dep.department_id==_user.Department)
.FirstOrDefault()
)
)
.Select(com => new { com.company_id, com.Country })
.FirstOrDefault();
到目前为止,我有:
flexbox
答案 0 :(得分:1)
使用基于方法的语法,这可以很简单;
var company = context.Companies
.Where(x => x.Departments
.Any(x => x.department_id == _user.Department)
)
.Select(x => new { x.company_id, x.Country });
这假设您的表设置了外键,为每个Company
提供了Department
个对象的列表。这在很大程度上取决于您的数据结构。