LINQ Find matching rows from two datables and copy to results to new datatable

I will need to return rows from DT1 which match with DT2 like following

DT1 column “Manufacturer” must match DT2 column “Mfr_No”
DT1 column “XX00” must be diffrent than column DT2 “YY00”
All other columns values on both datatables can be anything.

Is this possible? After we have result then return assign these rows to DT3.

@ajh
Welcome to the forum

Give a start try by following:

Variable: dtResult | DataType: DataTable

Assign activity
LHS: Result | DataType: List(of Datarow)
RHS

(From d1 in dt1.AsEnumerable
Join d2 in dt2.AsEnumerable
On d1("Manufacturer").toString.Trim Equals d2("Mfr_No").toString.Trim
Where Not d1("XX00").toString.Trim.Equals(d2("YY00").toString.Trim)
Select r = d1).toList

then check within an if acitvity
Result.Count > 0
Then: dtResult = Result.CopyToDataTable
Else: dtResult = dtData.Clone

That is working just fine thanks. What about if I want to include some columns also from d2 to the new datatable? All columns from d1 and for example “col1” and “col2” from d2

in that case we can do following:

prepare an empty datatable with Build datatable activity and configure the target datatable column structure (dt2 cols are on the end) - dtTarget

Assign activity, if logic as above

modified LINQ:

(From d1 in dt1.AsEnumerable
Join d2 in dt2.AsEnumerable
On d1("Manufacturer").toString.Trim Equals d2("Mfr_No").toString.Trim
Where Not d1("XX00").toString.Trim.Equals(d2("YY00").toString.Trim)
Let ra = d1.ItemArray.Concat(new Object(){d2("col1"), d2("col2")}).toArray
Select r = dtTarget.Rows.Add(ra)).toList
1 Like

Thanks a lot this is working also. Another question not related to previous one; is it possible to use LINQ to calculate price difference at one datatable between two columns and assign difference to difference column:

columns:
price
new_price
difference

I can do this using for each but is it possible to do also using LINQ

Perfect, so it is working and you can close the topic by:
Forum FAQ - How to mark a post as a solution - News / Tutorials - UiPath Community Forum

we prefer on having scoped 1 toic = 1 case, as researchers can find more easy a solution mapping to their case. But quick answerred:

  • in general yes, we can do with some LINQ techniques.
  • on selected cases we can also use other options

An introduction to the different options you can check here:
How to Update Data Column Values of a Data Table | Community Blog

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