0

I am new to C#. I am just looking for a function which is similar to the "merge" function in python, I've struggled a lot but still don't find an easy way.

My objective is to simply merge two tables to one table (keeping the column names), below is example:

Table #1: Person

enter image description here

Table #2: Car

car_number  car_color
111         black
122         red

Target output table:

enter image description here

I have tried LINQ

var results = from table1 in person.AsEnumerable()
                                  join table2 in car.AsEnumerable() on Convert.ToInt32(table1["car_number"]) equals Convert.ToInt32(table2["car_number"])
                                  select new { table1, table2 };

But here the trouble is that the CopyToDataTable() function is no longer supported. And it is difficult to save the results to datatable keeping column names.

I also checked the function "Merge" in C#, but it seems it requires a column of primary keys which contains only unique values.

halfer
  • 19,824
  • 17
  • 99
  • 186
victor_gu
  • 249
  • 1
  • 3
  • 9

0 Answers0