How to add values together from different datables

We do understand the requirement as following
dt1:Col1=A:Col2=1 + dt2:Col1=A:Col2=3 == dtResult:Col1=A:Col2=4
And so on for the others rows and cols

You can do it as following

Merge all datatables into one datatable (e.g. Merge datatable) - dtCombined
Then calculate the sums grouped by Col1 into a new datatable - dtResult:

(From d In dtCombined.AsEnumerable
Group d By k=d("Col1").toString.Trim Into grp=Group
Let rs = {1,2,3,4}.Select(Function (x) grp.Sum(Function (g) CInt("0" & g(x).toString.Trim))).Cast(Of Object).toArray
Let ra = rs.Prepend(k).toArray
Select r=dtResult.Rows.Add(ra)).CopyToDataTable

For more details on GroupBy have a look here:

here you will find also options on how to group data and processing the group members with a No LINQ or LINQ-For each approach

1 Like