How to sum up the Amount of Dublicate Id's in Data table In Linq

Hi Team,

Here i am having the input data where the “ID” are repetitive so, here i need to sum up all the Duplicate ID Amount i.e. (GROSS and Net),

Please find the below screen shots

Input:

Input_Dt

Required Output:

output_Dt

Input File:

Data.xlsx (9.3 KB)

Thanks in Advance.
Adarsh.

Hey @adarsh_kotagiri ,

You can refer to this video.

Thanks,
Sanjit

1 Like

Hi @adarsh_kotagiri,

Please try this code in the assign statement and let me know if it works,

(From row In dt.AsEnumerable
Group row By key = New With { Key .group = row.Item(“ID”) } Into grp = Group
Select dt.LoadDataRow(New Object() {
key.group,
grp.Sum(Function(row) Convert.ToDouble(row.Item(“GROSS”).ToString)),
grp.Sum(Function(row) Convert.ToDouble(row.Item(“NET”).ToString))
}, True)
).CopyToDataTable

Regards.

1 Like

Hi @adarsh_kotagiri

wha about the following?

(From row In dtInput.AsEnumerable
Group row By sc = row("ID").ToString()
Into grp = Group
Let totalGross= grp.sum(Function (x) Convert.ToDouble(x("GROSS").ToString()))
Let totalNet= grp.sum(Function (x) Convert.ToDouble(x("NET").ToString()))
Let result = New Object() {grp(0)("ID"),totalGross, totalNet}
Select dtToWrite.rows.add(result)).CopyToDataTable

Regards

1 Like

Hi @fernando_zuluaga
Thank you for your time. its working fine now.

Regards,
Adarsh.

1 Like

Glad to hear that, remember to mark as solution!

Regards!

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