Solved

Group By Using Linq

Posted on 2014-03-03
3
234 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:Varshini S
  • 2
3 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39900634
Hi Rubha;

Are you wishing to group on Designation or Department?
0
 

Author Comment

by:Varshini S
ID: 39900663
Department
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39900685
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question