2015-09-26 37 views
0

我想寫一個查詢來獲取與該部門相關員工的不同部門和數量(計數),但我無法找到確切的查詢在LINQ:集團按部門,並得到</p> <p>目前在該部門的員工人數

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

顯示你已經嘗試過 –

+0

編輯你的問題(不在評論中) –

回答

1

您可以使用:

var groupedEmploeesByDepartment = empdep.GroupBy(x => x.Department).Select(x => new { Department = x.Key, EmployeesCount = x.Count() }); 

現在的信息是IEnumerable的由Department和定義匿名類型屬性。

相關問題