我正在尝试编写一个查询来获取与该部门相关的不同部门和员工数量(计数)
但是我无法在linq中找到确切的查询:
var empname = new List<EmpName>();
var empadd = new List<EmpAddress>();
var empsal = new List<EmpSalary>();
var empdep = new List<EmpDepartment>();
empdep.Add(new EmpDepartment { EmployeeID = 101, Department = "CIV" });
empdep.Add(new EmpDepartment { EmployeeID = 105, Department = "CIV" });
empdep.Add(new EmpDepartment { EmployeeID = 106, Department = "CIV" });
empdep.Add(new EmpDepartment { EmployeeID = 102, Department = "AEI" });
empdep.Add(new EmpDepartment { EmployeeID = 107, Department = "AEI" });
empdep.Add(new EmpDepartment { EmployeeID = 103, Department = "IES" });
empdep.Add(new EmpDepartment { EmployeeID = 202, Department = "IES" });
empdep.Add(new EmpDepartment { EmployeeID = 203, Department = "IES" });
empdep.Add(new EmpDepartment { EmployeeID = 104, Department = "PIE" });
empdep.Add(new EmpDepartment { EmployeeID = 208, Department = "PIE" });
var results = (from d in empdep
group d.Department by d.EmployeeID into g
select new EmpDetails
{
Department = g.Key.ToString(),
DeptCount = g.Count()
}).ToList();
答案 0 :(得分:1)
您可以使用:
var groupedEmploeesByDepartment = empdep.GroupBy(x => x.Department).Select(x => new { Department = x.Key, EmployeesCount = x.Count() });
现在,信息位于由Department
和EmployeesCount
属性定义的匿名类型的IEnumerable中。