Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have one table and i want to show a list based on department.

how can i write group by query in linq in c# ?


linq is little bit new thing for me ?
Posted
Updated 8-Jan-14 1:57am
v2
Comments
Karthik_Mahalingam 8-Jan-14 7:57am    
post your code.
TrushnaK 8-Jan-14 8:12am    
improve your question and post your table structure and post what you want exactly.

 
Share this answer
 
v2
 
Share this answer
 
Try something Like...

C#
var q = from c in table
   group c by c.Department;


or visit here for an example of group by clause
 
Share this answer
 
v2
Comments
Christian Graus 8-Jan-14 16:47pm    
Nice to see someone gave code instead of just a link....
Suppose You have an EMPLOYEE table and want to sum of salaries as per employee name and have a data context class EmployeeOperationDataContext so create data context class object. This object contains employee data.

C#
EmployeeOperationDataContext employee = new EmployeeOperationDataContext(); 
 var salarySum = from emp in employee.EMPLOYEEs
                 group emp by emp.Name into empg
                 select new { NAME = empg.Key, SALARY = empg.Sum(x => x.SALARY)};
 
Share this answer
 

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