Monday 24 November 2014

How to get group wise data from Datatable in C#?


Hi, If you face the problem  till now, don't worry I have a solution.,

if you have the data like this ....

id|empName |designation
-----------------------------------
1 |john    |manager 

2 |manu    |ast.manager 
3 |mathew  |accountant 
4 |sree    |ast.manager

And need to group this by designation?  The given LINQ C# code will help you to solve this



var EmData = from row in dtEmpData.AsEnumerable()
group row by new
{
des = row.Field<string>("designation")

} into g
select new
{
designation = g.Key.des,
ColumnValues = g
};


:) By using two for each statements will help you guys to traverse through these record

foreach (var k in EmData)
{
      // you can access designation by using   k. designation


     foreach (var columnValue in k.ColumnValues )
    {
      // access group by result [Employee details] through this expression   columnValue["id"]
     } 

}


:) Thank you.

No comments:

Post a Comment