Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to compare three datatables? The three datatables will have the same columns, but I want to check the rows of SNAME which are not in the rest of the tables. Also, the values of the SNAME column and its respective all column values in all three are to be displayed in the output table as shown below. Is there a tempting way to implement this?

Table1

DIP   | SNAME |  T1_350 |  T1_1000 | 
-------------------------------------
GOOD  |  A001 | 225.365 |  338.047 |
GOOD  |  A002 | 119.254 |  445.254 |
GOOD  |  A003 | 145.212 |  365.236 |
GOOD  |  A004 | 265.958 |  330.558 |
GOOD  |  A005 | 235.908 |  340.568 |

Table2

DIP   | SNAME |  T2_350 |  T2_1000 | 
-------------------------------------
GOOD  |  A001 | 244.223 |  368.554 |
GOOD  |  A002 | 112.211 |  487.587 |
GOOD  |  A003 | 225.565 |  302.548 |
GOOD  |  A004 | 154.542 |  421.364 |
GOOD  |  A005 | 166.254 |  365.356 |

Table3

DIP   | SNAME |  T3_350 |  T3_1000 | 
-------------------------------------
GOOD  |  A001 | 230.203 |  378.554 |
GOOD  |  A002 | 114.251 |  477.547 |
GOOD  |  A003 | 295.545 |  342.598 |
GOOD  |  A005 | 136.294 |  375.366 |

Output Should be:

TableOut
DIP   | SNAME |  T1_350 |  T1_1000 | T2_350  | T2_1000  | T3_350  | T3_1000 |
-----------------------------------------------------------------------------
GOOD  |  A001 | 225.365 |  338.047 | 244.223 |  368.554 | 230.203 | 378.554 |
GOOD  |  A002 | 119.254 |  445.254 | 112.211 |  487.587 | 114.251 | 477.547 | 
GOOD  |  A003 | 145.212 |  365.236 | 225.565 |  302.548 | 295.545 | 342.598 |
GOOD  |  A005 | 235.908 |  340.568 | 166.254 |  365.356 | 136.294 | 375.366 |


What I have tried:

What I have tried:

C#
DataRow drRow; 
for(int i=0;i<=dt3.Rows.Count;i++)
{
  drRow = dtOut.NewRow();
  drRow["DIP"] = dt3.Rows[i]["DIP"].ToString();
  drRow["SNAME"] = dt3.Rows[i][1].ToString();
  drRow["T1_350"] = dt1.Rows[i][2];
  drRow["T1_1000"] = dt1.Rows[i][3]; 
  drRow["T2_350"] = dt2.Rows[i][2];
  drRow["T2_1000"] = dt2.Rows[i][3]; 
  drRow["T3_350"] = dt3.Rows[i][2];
  drRow["T3_1000"] = dt3.Rows[i][3]; 
  dtOut.Rows.Add(drRow);
  dtOut.AcceptChanges();
  
}

It is not working as expected. It mismatches values due to SNAME missing a rows value. Please suggest a better solution. Thanks in advance.
Posted
Updated 28-Jan-22 6:09am
v2

I expect your code to fail as it implicitly assumes one row index in three tables will produce the same SNAME value, which your example data does not.

You seem to want an INNER JOIN of three DataTables?

With database tables and SQL that should be straightforward.

With DataTables, you can achieve the same; and LINQ can be applied as shown here: Inner join of DataTables in C# - Stack Overflow[^]

:)
 
Share this answer
 
v3
For environment setting and set-based operations with challenges of non-keyed tables.
to perform row-to-row changes, kindly review the following link:

https://www.sqlshack.com/compare-tables-sql-server/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900