To group the name and sumup the it's amount without looping

Dt1 :
Name Amount
Apple. 10
Nokia. 50
Apple. 40
Vivo. 20
Vivo. 10

Here to group name and total the amount of respective names.the want output like

Dt2:

Name. Amount
Apple. 50
Nokia. 50
Vivo. 30

@brindhaS
Assign: left Side new Var DataTable: dtResult, right side: OriginalDataTableVar.Clone

Assign:
left side: dtresult
right side:

(From d in OriginalDataTableVar.AsEnumerable
Group d by k=d(0).toString.Trim Into grp=Group
let res = grp.Sum(Function ® Cint(r(1).toString.Trim))
Select dtResult.Rows.Add({k,res})).CopyToDataTable

2 Likes

That works friend thanks but it taking only that two columns I having totally 6 columns along with it.what to do for it any change in that code u given also it roundup the value point value

How can we learn to write such query to solve our problems?
Most of the beginners don’t know about it.

@ppr
There are more columns in my dt not only this two columns
ID Name data Details Amount
12 Apple. Data’s. Details 50
34. Nokia. Data. Details. 50
45. Vivo. Good. Details. 30
I want to get like this .please tell me any minor change in the code you have given

@brindhaS
Doing all in a LINQ and bring this dynamics in it (Statics, Aggregations) will result in less maintainable solution.

Find a demo balancing this both. As in a first go all statics are assigned, then only Assignments for the Custom Aggregations is needed. (Think on 25 Cols, 23 static)

I adopted for you to do the sums on doubles and schow cased on String Aggregations as well

Demo is here:
GrouBy_Custom_Statics-Aggs.xaml (11.9 KB)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.