Group By Using Linq

I have the below object  List<Employees>
EmployeeID
Name
Designation
DOB
DOJ
Department


How do I use linq to group by the output like below

Designation      NoOfEmployees
Sales                   250
Marketing          100
Finance               25
HR                       15
Varshini SAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Rubha;

This should give you what you need.

List<Employees> empList = new List<Employees>();
// ... List gets filled


var results = from emp in empList
              group emp by emp.Department into deptGroup
              select new
              {
                  Designation = deptGroup.Key,
                  NoOfEmployees = deptGroup.Count()
              };

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi Rubha;

Are you wishing to group on Designation or Department?
0
 
Varshini SAuthor Commented:
Department
0
All Courses

From novice to tech pro — start learning today.