Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy Won't Two Indentical DataTables Show Any Differences?
    primarykey
    data
    text
    <p>I am using <code>DataRelation</code> to compare two <code>DataTable</code> and return a third table containing the rows that are not share by both original tables by using this <a href="http://weblogs.sqlteam.com/davidm/archive/2004/01/19/739.aspx" rel="nofollow noreferrer">tutorial</a>. Below is the code.</p> <pre><code>public static DataTable Difference(DataTable First, DataTable Second) { //Create Empty Table DataTable table = new DataTable("Difference"); //Must use a Dataset to make use of a DataRelation object using(DataSet ds = new DataSet()) { //Add tables ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()}); //Get Columns for DataRelation DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count]; for(int i = 0; i &lt; firstcolumns.Length; i++) { firstcolumns[i] = ds.Tables[0].Columns[i]; } DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count]; for(int i = 0; i &lt; secondcolumns.Length; i++) { secondcolumns[i] = ds.Tables[1].Columns[i]; } //Create DataRelation DataRelation r = new DataRelation(string.Empty,firstcolumns,secondcolumns,false); ds.Relations.Add(r); //Create columns for return table for(int i = 0; i &lt; First.Columns.Count; i++) { table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType); } //If First Row not in Second, Add to return table. table.BeginLoadData(); foreach(DataRow parentrow in ds.Tables[0].Rows) { DataRow[] childrows = parentrow.GetChildRows(r); if(childrows == null || childrows.Length == 0) table.LoadDataRow(parentrow.ItemArray,true); } table.EndLoadData(); } return table; } </code></pre> <p>Surprisingly enough when I exercised this code, it only worked on few cases and failed in others.</p> <p>There was one case, I was comparing two identical table (but has different source) that both has:</p> <ul> <li>Same Column Name &amp; <code>DataType</code></li> <li>Same Number of Rows &amp; Columns</li> <li>Same Value in Each Cells</li> </ul> <p>Only to have a brand <strong>new table returned that actually has the exact same composition</strong> as the two original tables!</p> <p>What did I <strong>possibly miss</strong>?</p> <p>Is it possible that two <strong>identical tables</strong> (sharing features I mentioned above) can have <strong>other different properties</strong> (which doesn't seem visible to users' eyes)?</p> <p>Or is it possible that this is actually a <strong>bad method</strong>? What are the possible alternatives?</p> <p>Any advise is highly appreciated.</p> <p>Regards</p> <p><strong>EDITED</strong></p> <ol> <li>Both tables have same primitive datatype such as: <code>System.String</code>, <code>System.Int32</code>, <code>System.DateTime</code></li> <li>These code doesn't work on all samples I tested</li> <li>Here is a print screen of 1 sample (using <code>DataSet</code> Visualizer)</li> </ol> <p><img src="https://i.stack.imgur.com/cpjjg.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload