Null Reference Exception while joining DataTables using linq

Hi All,

I’m trying to join two datatables using linq operations. The operations throws me null reference exception not sure what I’m doing wrong. Have attached the xaml file for reference.

DataTable1 : DataDT
ID Name City Age Hobbies
|2|Sam|Mumbai|22|Guitar|
|3|Ham|Delhi|12|Base*|
|4|Kam|Chennai|33|Nothing|

DataTable2 : DT2
Sales ID Name City Age Hobbies Salary
|2|Sam|Mumbai|22|Guitar|334|
|3|Ham|Delhi|12|Base*|44|
|6|Kam|Chennai|33|Nothing|555|

Linq query used: Result = (From tb1 In DataDT.AsEnumerable() _
Join tb2 In DT2.AsEnumerable() _
On tb2.Field(Of Double)(“Sales ID”) Equals tb1.Field(Of Double)(“ID”)
Select Result.LoadDataRow(New Object(){tb1.Field(Of String)(“City”)},False)).CopyToDataTable

Data.xlsx (8.8 KB)

Linq.xaml (8.4 KB)

Any kind of assistance would be helpful. Thank you in advance

1 Like

Try this.
Please create one new data table say dtResult and initialize.

dtResult = new DataTable

Result = (From tb1 In DataDT.AsEnumerable() _
Join tb2 In DT2.AsEnumerable() _
On tb2.Field(Of Double)(“Sales ID”) Equals tb1.Field(Of Double)(“ID”)
Select dtResult.LoadDataRow(New Object(){tb1.Field(Of String)(“City”)},False)).CopyToDataTable

Please notify the change in the Select clause.

Regards,
Karthik Byggari

4 Likes

Hi karthik,

Thank you for the prompt reply. It worked :grinning:

1 Like

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