Linq for Substraction of 2 columns of 2 datatables

Hi Team,

I am having an issue while writing a linq,

The Condition being:

for each row in dt1
{
for each row in dt2
{
if
{
dt1.row(0)=dt2.row(1)
then
{
dt1.row(5)=dt1.row(5)-dt2.row(7)
}}}}

dt1 and dt2 being 2 different datatables.
subtracting one column of one datatable with another column of another datatable if a condition is satisfied.

Thanks in Advance.

Hi @yash.choursia

Can you please provide input excel file, meanwhile try below code

resultDt=(From rowDt1 In dt1.AsEnumerable()
                From rowDt2 In dt2.AsEnumerable()
                Where rowDt1.Field(Of String)(0) = rowDt2.Field(Of String)(1)
                Select dt1.Clone().Rows.Add(rowDt1.ItemArray.Select(Function(value, index) If(index = 5, Convert.ToDouble(value) - Convert.ToDouble(rowDt2.Field(Of Double)(7)), value)).ToArray())).CopyToDataTable()

Regards,

@yash.choursia

 (From row1 In dt1.AsEnumerable()
                   From row2 In dt2.AsEnumerable()
                   Where row1.Field(Of String)(0) = row2.Field(Of String)(1)
                   Select dt1.Clone().Rows.Add(row1.ItemArray.Select(Function(item, index) If(index = 5, CObj(CDec(item) - CDec(row2.ItemArray(7))), item)).ToArray())).CopyToDataTable()

Where row1.Field(Of String)(0) = row2.Field(Of String)(1) here my comparision datatype is string give your Datatype which you want to compare

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