Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a datatable having values

Date value
1/7/11 100
2/7/11 10
2/7/11 700
3/7/11 40

I want the datatable as

Date value
1/7/11 100
2/7/11 710
3/7/11 40

i want to sum the value column havind duplicate rows

Any help wil b appreciated

regards
chinnu
Posted
Updated 30-Nov-17 21:01pm

var result = dt_UnSort.AsEnumerable().GroupBy(dr => dr.Field<string>("Datevalue")).Select(group => new { Datevalue= group.Key, Count = group.Sum(col => col.Field<int>("Count")) });

                DataTable dt_Sorted = dt_UnSort.Clone();
                foreach (var grp in result)
                    dt_Sorted.Rows.Add(grp.Datevalue, grp.Count);
 
Share this answer
 
v3
Comments
Kats2512 1-Dec-17 3:35am    
6 year old question that has already got an accepted solution...
Member 13528638 31-Dec-17 15:47pm    
This helped me :D
hi Al Moje

i used Dictionary<key,value>
 
Share this answer
 
Hi,

Try this if could help...
DataTable x = new DataTable();
         IEnumerable<DataRow> dtrow = x.AsEnumerable();
         var dt = dtrow.GroupBy(dtl => dtl.Field<string>("TransDate"))
             .Select(dtg => new dtg
             {
                 dtl.Field<string>("TransDate") = dtg.Key,
                  dtrow.Sum(d => d.Field<double>("Amount"))
             }).ToList();



Regards,

Algem
 
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