LINQ to Add Column from one DataTable to Another

I have two data tables and I want to grab only one column from one of the data tables and add it to the other one. How can I accomplish this in a simple way with LINQ queries, without using a For Each?

Example:
DT1 has column “Name” with two rows
DT2 has the same number of rows, but does not have column “Name.” I want column “Name” and all row values to be copied into DT2.

Hi @benjiherb

Could you share input excels with sample data what you want to compare.

Regards

I don’t really have an example Excel. I simply just want to copy a full column (and its values) from DT1 to DT2.

Is there a common value between the two tables that you’re trying to match up? Or don’t you care about the order of the rows in the two datatables?

If there is a common value you need to match on, then use Join Data Table.

If you don’t care about the order, then just use Filter Data Table to keep only the column you want in DT1, and then use Merge Data Table to copy it to DT2.

If you want to use Linq query and does not having any common columns in both the table, You can use below query in a Assign activity

(From row1 In dt1.AsEnumerable()
From row2 In dt2.AsEnumerable()
Select resultRow = dt3.Rows.Add(row1(“Name”), row2(“Number”))).CopyToDataTable()

Let me know if you need sample XAML file.

Assumption: Project is set to Windows Compatibility
For learning purpose see below LINQ Approach (DataTable Reconstruction Strategy)

Assign Activity:
DT2Enhanced = dt2.Clone

Prepare DT2Enhanced by adding the Name column at the end with add DataColumn

Assign activity:
DT2Enhanced=

(From i in Enumerable.Range(0,dt2.AsEnumerable.Rows.Count
Let ra1 = dt2.Rows(i).ItemArray
Let n = dt1.Rows(i)("Name")
Let ra = ra1.Append(n).ToArray
Select r = DT2Enhanced.Rows.Add(ra)).CopyToDataTable

Feel free to reorder the DT2Enhanced columns by DT2Enhanced.DefaultView.ToTable(False, arrColNames)

arrColNames is String Array with all ColNames in new order