Im using linq to get an output sheet named output_dt from two different sheets, sheet1 and sheet2.
I’m getting all the columns from sheet1 in my output_dt sheet which is expected, but along with that I want one column from sheet2.
How to achieve this?
Linq query is
(From D1 in DT1.asEnumerable
From D2 in DT2.AsEnumerable
Where Condition select D1).copytodatatable
(From D1 in DT1 Join D2 in DT2 on condition
Select output.clone.LoadDataRow(New Object(){D1(ColumnName),D1(ColumnName)…,D2(ColumnName)},False)).CopyToDataTable
where output is the new datatable to which u are assigning join results
some details are unclear but keep in mind:
From D1 in DT1.asEnumerable
From D2 in DT2.AsEnumerable
is doing a cartesian product. Not sure if this was your intention.
Lets have a look on following example:
dt1:
dt2
Task:
left join on first columns
take all cols from dt1 and the comment col from dt2 for the matchers
flow:
construct the empty result datatable with the needed column structure
use a LINQ
(From d1 In dt1.AsEnumerable
Join d2 In dt2.AsEnumerable
On d1(0).toString Equals d2(0).toString
Let ra = d1.ItemArray.Append(d2("Comment")).toArray
Select dtResult.Rows.Add(ra)).CopyToDataTable
with the help of append we can construct the new itemArray (datarow object array) easy:
take all columns from d1 by grabbing the d.ItemArray. Append the Comment col to the Object Array and return it as an object array, late be used for the result rows addition
By using the itemArray we get all columns from dt1 without explicit listing it.
Hi @ppr,
Thank you for replying.
I don’t want to use join as I’m using the condition which checks if the values of sheet 1 is equals to the values of sheet 2 and then writes in the sheet3. I can’t use join activity for that.