0

I have 2 data tables with similar data but totally different id fields. the data columns have different names too This is what I mean:

Table1

 id      dataIn1
010      Data000
123      Data1   
222      Data2
323      Data4
443      Data6

Table2

 id      dataIn2
551      Data1
676      Data2
111      Data3
625      Data4
444      Data5
665      Data6

I want a new DataTable3 that has data in Table2 but not in Table1. Note that there is a row in Table1 not in Table2 as well but I don't care about it.

Table3
 id      dataIn3
111      Data3
444      Data5

I have tried these approaches:

tb2.Merge(tb1);
DataTable tb3 = tb2.GetChanges(); 

But tb3 returned null

And this returned a datatable but the rows were wrong

var notIn1 = tb2.AsEnumerable().Select(r => r.Field<string>("dataIn2"))
                .Except(tb1.AsEnumerable().Select(r => r.Field<string>("dataIn2")));

DataTable tb3 = (from row2 in tb2.AsEnumerable()
                 join row3 in notIn1
                 on row2.Field<string>("dataIn2") equals row3
                 select row2).CopyToDataTable();
PTN
  • 1,658
  • 5
  • 24
  • 54

1 Answers1

2

You can do that like this:

IEnumerable<int> val_table1 = Table1.AsEnumerable().Select(val=> (int)val["dataIn1"]);
IEnumerable<int> val_table2  = Table2.AsEnumerable().Select(val=> (int)val["dataIn2"]);
IEnumerable<int> val_notinTable1= val_table2.Except(val_table1);
freshbm
  • 5,540
  • 5
  • 46
  • 75