如何在entityframework LINQ方法语法中应用内连接?

时间:2015-08-12 10:34:38

标签: entity-framework-4 inner-join

如何在实体框架方法语法中应用此查询

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

1 个答案:

答案 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个对象的列表。这在很大程度上取决于您的数据结构。