Group By in DataTable

Posted by Write2varun under ASP.NET on 1/24/2014 | Points: 10 | Views : 1019 | Status : [Member] | Replies : 3
Hi,
I have a DataTable with 3 Records

col1 | col2 | col3 | col4 | col5|
1 |abc |7 | | |
1 |abc | | 9 | |
1 |abc | | |6 |


I want to group the data in the format


col1 | col2 | col3 | col4 | col5|
1 |abc |7 |9 |6 |


i Have to do it in the .cs page I can't do it SQL

Thanks




Responses

Posted by: vishalneeraj-24503 on: 1/24/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please refer:-

http://www.codeproject.com/Questions/111761/DataTable-compute-with-group-by
http://maulikdhorajia.blogspot.co.nz/2010/08/apply-group-by-clause-on-datatable-in-c.html
http://sutocom.net/2013/09/04/apply-group-by-clause-on-datatable-in-c/
http://stackoverflow.com/questions/7736831/group-by-in-datatable-column-sum

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 1/24/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Also refer:
http://www.c-sharpcorner.com/Blogs/10258/grouping-datatable-column-using-linq-in-C-Sharp.aspx
http://stackoverflow.com/questions/7736831/group-by-in-datatable-column-sum

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/24/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Try like this.

var lst=(from r in YOURTABLE
group r by new { r.col1 } into g
let col01=g.Sum(x=>x.col3)
let col02=g.Sum(x=>x.col4)
let col03=g.Sum(x=>x.col5)
select new{r.col1,r.col2,col01,col02,col03}).ToList();

Thank you,
Govind

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response