2

我有以下类结构。

Company > List(of Departments) > List(of Employees)

我想查询一家公司,看看它是否有一个具有以下名称的部门以及该部门中具有以下 ID 的员工!

我怎么能查询到这个。我拥有它期望返回一个部门的代码的方式,所以我有它做

If Company.Where(function(d) d.Name = 'ABC').First.Where(function(e) e.EmployeeId = 1).Count > 0 

但这将错误 if d.Name = 'ABC'返回 0 条记录(我希望它返回 1 或 0 条记录!)

linq是否有可能在一个声明中做到这一点?

4

2 回答 2

4

assuming that a company has multiple departments and a department has multiple employees-- then you could do this.. sorry I'm not using vb syntax:

DB.Departments.Where(d=>d.Name=="ABC").SelectMany(d=>d.Employees).Any(e=>e.EmployeeId == 7)

this would tell you if a specific employee was in a specific department name at any of the companies.

于 2012-04-17T15:47:34.353 回答
2
var employeeExists = (from company in companies
   where company.Name = expectedCompanyName
   from department in company.Departments
   from employee in department.Employees
   where employee.Id == expectedEmployeeId
   select employee.Id).Any()
于 2012-04-17T15:59:36.640 回答