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

how to apply inner join in entityframework LINQ method syntax?

如何在 entity framework 方法语法中应用此查询

select company_id, country_id from companies, departments where companies.company_id == departments.company_id and department.departmentId = 10;

到目前为止,我有:

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();

使用基于方法的语法,这可以很简单;

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