查询以对员工进行计数并显示拥有一名以上员工的部门

时间:2019-04-20 06:32:29

标签: c# sql asp.net linq core

我需要查询以使用LINQ显示所有部门,其位置和雇员人数,其中雇员人数> 1。

我在Department模型中具有ICollection属性。

部门表:

departmentId | departmentLocation
      1             London
      2             Paris
      3             New York

员工表:

employeeId | employeeName |departmentId
      1           John         3
      2           Mary         2
      3           Steve        3

非常感谢。

1 个答案:

答案 0 :(得分:1)

如果您在public virtual ICollection<Employee> Employees { get; set; }模型中拥有Department属性,则:

var result = context
                .Department
                .Where(d => d.Employees.Count > 1)
                .Select(d => new
            {
                d.departmentId,
                d.departmentLocation,
                NumberOfEmployees = d.Employees.Count
            }).ToList();
相关问题