Assign: Specified cast is not valid. Merge Data Table

This is sample data from 2 data tables.

dtEmployees
image

dtSalaries
image

My LINQ Code :

(From e In dtEmployees.AsEnumerable()
Join s In dtSalaries.AsEnumerable()
On e.Field(Of String)(“EmployeeID”) Equals s.Field(Of String)(“EmployeeID”)
Select dtJoined.LoadDataRow(New Object() {
e.Field(Of String)(“EmployeeID”),
e.Field(Of String)(“Name”),
s.Field(Of Decimal)(“Salary”)
}, False)).CopyToDataTable()

Why i got error Assign: Specified cast is not valid ?

give a try on this modified line

On e("EmployeeID").ToString.Trim Equals s("EmployeeID").toString.Trim

UPD1 - Additional hints
when the issue persist then:

  • check all column datatypes from dtEmployees, dtSalaries
  • ensure that there are of same type and dtJoin has modell it as the same

try the following:

(From e In dtEmployees.AsEnumerable()
Join s In dtSalaries.AsEnumerable()
On e("EmployeeID").ToString.Trim Equals s("EmployeeID").toString.Trim
Let ra = new Object(){e("EmployeeID"),e("Name"), s("Salary") }
Select r=dtJoined.LoadDataRow(ra, False)).CopyToDataTable()

I tried and now the error is…
Assign: Object reference not set to an instance of an object.

we updated with an additional hint

what was done in detail?

ensure / trace the variables and definition e.g. with:
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

let not mix up LINQ, Data, Flow and modellings as a correct LINQ can fail e.g.

  • used on the wrong place
  • used with variation of data, which are out of expectactions

Hi @Enrico_N_F

can you try with below code. incase if your row having no value it will skip it.

Dim query =
From e In dtEmployees.AsEnumerable()
Join s In dtSalaries.AsEnumerable()
On e.Field(Of String)(“EmployeeID”) Equals s.Field(Of String)(“EmployeeID”)
Select dtJoined.LoadDataRow(New Object() {
e.Field(Of String)(“EmployeeID”),
e.Field(Of String)(“Name”),
s.Field(Of Decimal)(“Salary”)
}, False)

Dim joinedData = query.CopyToDataTable()
Hope it helps!!!