2012-03-29 39 views
2

我正在使用DataRelation來比較兩個DataTable,並使用此tutorial返回包含兩個原始表不共享的行的第三個表。以下是代碼。爲什麼兩個Indentical DataTables不顯示任何差異?

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 < firstcolumns.Length; i++) 
      {  
        firstcolumns[i] = ds.Tables[0].Columns[i];  
      } 

      DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count]; 

      for(int i = 0; i < 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 < 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;  
} 

令人驚訝的是,當我行使這個代碼,它只是工作在少數情況下,而在其他失敗。

有一種情況下,我比較兩個相同的表(但是具有不同的源),這兩個具有:

  • 相同的列名& DataType
  • 行的數目相同&列
  • 相同值in Each Cells

只有一個品牌新表返回,其實h如同兩張原始表格完全相同的組合

我做了什麼可能錯過

是否有可能兩個相同的表(我上面提到的共享功能)可以有不同性質(這似乎並沒有對用戶的眼睛可見的)?

還是有可能這實際上是一個不好的方法?什麼是可能的選擇?

任何意見是高度讚賞。

問候

EDITED

  1. 兩個表都有相同的原始數據類型,如:System.StringSystem.Int32System.DateTime
  2. 這些代碼不會對我測試
  3. 這裏所有樣本工作是1個樣品的打印屏幕(使用DataSet展示臺)

enter image description here

+1

這些是什麼列的數據類型?它只是原始的嗎?也許它們包含一些通過引用不相同的對象? '(...)但有不同的來源(...)'可能是這裏的關鍵。你應該知道,像(例如)'var bitmapA = Bitmap.FromFile(@「C:\ picture.jpg」); var bitmapB = Bitmap.FromFile(@「C:\ picture.jpg」);' - 'bitmapA == bitmapB'爲'false' – 2012-03-29 09:38:17

+0

理論上這個方法應該可以工作。在預期的匹配不起作用的情況下,是否按同一順序排列?你能提供一個不起作用的小例子嗎? – kaj 2012-03-29 10:31:55

+0

明天我會回到辦公室後=) – rofans91 2012-03-29 13:43:37

回答

1

我不得不寫類似的一次,這是我使用的方法:如果你沒有在每個表中重複的行

首先這種方法只適用。

使用主鍵..

First.PrimaryKey = firstcolumns; 
Second.PrimaryKey = secondcolumns; //These throw exceptions when you have duplicate rows 

然後..

foreach (DataRow dr in Second.Rows) 
{ 
    List<Object> l = new List<Object>(); 

    foreach (DataColumn dc in secondcolumns) l.Add(dr[dc]); 

    if (First.Rows.Find(l.ToArray()) == null) //NOT FOUND 
    { 
     table.Rows.Add(l.ToArray()); 
    } 
} 

foreach (DataRow dr in First.Rows) 
{ 
    List<Object> l = new List<Object>(); 

    foreach (DataColumn dc in firstcolumns) l.Add(dr[dc]); 

    if (Second.Rows.Find(l.ToArray()) == null) //NOT FOUND 
    { 
     table.Rows.Add(l.ToArray()); 
    } 
} 

乾杯,

相關問題