Linq query to compare two datatatables and sum column values of matching row

Hi Team,

I have 2 excels

excel1

Col1,Col2
A 4
B 5
C 6

Excel2
Col1,Col2
M 6
N 7
B 8

if Col1 of excel1 matches Col1 of excel2 add Col2

eg B matches in both then my answer shoulld be 8+5

new datable will be
col1,col2
B 13

regards,
supriya

Hi @supu123

Kindly refer the below solution,

Hope this will be helpful. Thank you.

@supu123

  1. Use Read Range activity to read the data from excel files and it will give output as DataTable. Let’s say InputDT1 and InputDT2.

  2. DataTable outputDT = InputDT1.Clone

  3. And then try below Linq query.

OutputDT = ( From a in InputDT1 Join b in InputDT2 On a("Col1").ToString Equals b("Col1").ToString Select OutputDT.Rows.Add({a("Col1"), Cint(a("Col1").ToString.Trim)+Cint(b("Col1").ToString.Trim)}))

1 Like

Hi

Error while using this formula

Thanks and Regards,
Supriya

@supu123

At the end of the expression write CopyToDataTable.

OutputDT = ( From a in InputDT1 Join b in InputDT2 On a("Col1").ToString Equals b("Col1").ToString Select OutputDT.Rows.Add({a("Col1"), Cint(a("Col1").ToString.Trim)+Cint(b("Col1").ToString.Trim)})).CopyToDataTable

1 Like

There is a small mistake in terms of Columns in the expression…

Updated:
OutputDT = ( From a in InputDT1 Join b in InputDT2 On a(“Col1”).ToString Equals b(“Col1”).ToString Select OutputDT.Rows.Add({a(“Col1”), Cint(a(“Col2”).ToString.Trim)+Cint(b(“Col2”).ToString.Trim)})).CopyToDataTable

Regards.

Hi @lakshman

Formula is working for me but facing a issue

modified the formula to get the number in the format ##,###.##

but in the excel it is throwing an error after write range

how to handle this

Thanks and Reagrds,
Supriya Yenaganti

more over excel is of format “.xls”