Hi, I have two tables with the same structure. The columns are ID1, ID2, ValueA, ValueB.
Now I would like to compare the rows with the same ID1 AND ID2 between the two tables and add the differences to a third table. (This shows an employee potential errors that he needs to check)
E.g.
Table 1:
ID1 ID2 ValueA ValueB
ABC EFG 2 4 XYZ ABC 3 9
SSC FDF 1 1
(From x In Table1.AsEnumerable() where Not (From a In ReadDT.AsEnumerable() Join b In Table2.AsEnumerable() On a(“ID1”).ToString() Equals b(“ID1”).ToString() and a(“ID2”).ToString() Equals b(“ID2”).ToString() select a).Contains(x) select x).CopyToDataTable()
And it gave me
aa dd 3 3
Whereas I wanted
aa cc 2 4 → because there the IDs are the same but the values are different
Sorry, if I didn’t make the problem clear in the beginning
EDIT: Like in the examples I would like to see differences in the column “ValueA” and “ValueB”. Rows should only be compared if their ID1 AND ID2 are equal.
The IDs have to match so that the values can be compared.
If both of the IDs of a row are the same in both tables then their values have to be the same (otherwise add to difference table).
But if the values are the same, the IDs do not necessarily have to be the same.
IDs could be shoe name and shoe size, then the values would be quantity in stock and price.
So if I look at a particular shoe in a particular size, then the values for stock and price have to be the same in both tables.
Let’s make the example a bit simpler then. Let’s assume there was only one column with a key and one with a value. Then one would take the first row in table1 and search for the same ID in table2. If the values of both rows are the same it is skipped, if the values are different, then the value of the table2 is added to the third table.
My previous example is the same only that the unique key consists of two IDs and there are two values to look at. The first value is compared with the first and the second with the second.
Match ID DT :- DT1 = (From x In Table1.AsEnumerable() Where (From a In Table1.AsEnumerable() Join b In Table2.AsEnumerable() On a(“ID1”).ToString() Equals b(“ID1”).ToString() And a(“ID2”).ToString() Equals b(“ID2”).ToString() Select a).Contains(x) Select x).CopyToDataTable()
Matches only the IDs first then,
Match Values DT :- DT2 = (From x In DT1.AsEnumerable() Where Not (From a In DT1.AsEnumerable() Join b In Table2.AsEnumerable() On a(“ValueA”).ToString() Equals b(“ValueA”).ToString() and a(“ValueB”).ToString() Equals b(“ValueB”).ToString() Select a).Contains(x) Select x).CopyToDataTable()
Try this, or the the code with one single query which has been shared in this page also can be used.