Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have employee details in DataSet. How to write Linq query to group employees by department and display them in grid?

What I have tried:

var names = from emp in dt.AsEnumerable()
                        group emp by new
                       {
                           ID = emp.Field<int>("UserId"),

                       } into g
                        select new
                        {
                            ID = g.Key.ID,
                        };
Posted
Updated 6-Mar-17 7:18am
Comments
Tomas Takac 6-Mar-17 7:09am    
Try group emp by emp.Field<int>("UserId") into g.

1 solution

If you would like to group data by department, you have to use proper field, such as DepartmentID:
C#
var names = dt.AsEnumerable()
    .GroupBy(emp=>emp.Field<int>("DepartmentId"))
    .Select(grp=> new
        {
             DeptId = grp.Key,
             //define other fields here
        });


For further details, please see:
101 LINQ Samples in C#[^]
Linq 101 lambda samples[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 6-Mar-17 22:14pm    
5 for the links
Maciej Los 7-Mar-17 1:50am    
Thank you, Karthik.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900