# Finding the total sum of 2 columns using LINQ

Good Day.

I have this datatable where I have to get the total sum of Time column and Cost column depending on its Project Code. I aim to use LINQ to be more efficient especially when processing thousands of rows.

Kindly see attached files for the desired output and excel file being used. Thank you.

Desired output: ExcelFileSample.xlsx (8.8 KB)

Best regards,
Robert Monsalud

1 Like

Thank you, Sir. I will try your solution

1 Like

It worked. Thank you so much, Sir.

In this case the group by approach can also be used ``````(
From row In dt_Data
Group row By k=row("Project Code").ToString.Trim
Into grp = Group
Select dt_Data.LoadDataRow({k, grp.Sum(Function(gr) CInt(gr("Time"))), grp.Sum(Function(gr) CInt(gr("Cost")))}, True)
).CopyToDataTable
``````

XAML for reference

TotalSumOf2Columns.xaml (5.6 KB)

1 Like

Thank you, Sir. This is a very great solution too. May I ask what if there will be 2 or more reference columns just like this:

and the desired output is something like this: What would be the possible work around?

Kindly see attached file for the excel file being used. Thank you.

ExcelFileSample.xlsx (9.0 KB)

Best regards,
Robert Monsalud

It is possible ``````(
From row In dt_Data
Group row By
k1=row("Skill Tribe").ToString.Trim,
k2 = row("Project Code").ToString.Trim
Into grp = Group
Select dt_Data.LoadDataRow({k1, k2, grp.Sum(Function(gr) CInt(gr("Time"))), grp.Sum(Function(gr) CInt(gr("Cost")))}, True)
).CopyToDataTable
``````

TotalSumOf2Columns.xaml (5.7 KB)

2 Likes

Thank you so much, Sir. This will be very helpful.

Best regards,
Robert Monsalud

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