C# 为什么赢了';两个相同的数据表是否显示出任何差异?

C# 为什么赢了';两个相同的数据表是否显示出任何差异?,c#,datatable,C#,Datatable,我正在使用DataRelation比较两个DataTable,并返回第三个表,其中包含两个原始表未共享的行。下面是代码 public static DataTable Difference(DataTable First, DataTable Second) { //Create Empty Table DataTable table = new DataTable("Difference"); //Must use a Dataset to make u

我正在使用
DataRelation
比较两个
DataTable
,并返回第三个表,其中包含两个原始表未共享的行。下面是代码

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;    
}
公共静态数据表差异(数据表第一,数据表第二)
{
//创建空表
数据表=新数据表(“差异”);
//必须使用数据集才能使用DataRelation对象
使用(数据集ds=新数据集())
{
//添加表
AddRange(新数据表[]{First.Copy(),Second.Copy()});
//获取DataRelation的列
DataColumn[]firstcolumns=新的DataColumn[ds.Tables[0].Columns.Count];
for(int i=0;i
令人惊讶的是,当我使用这段代码时,它只在少数情况下有效,在其他情况下失败

有一个案例,我比较了两个相同的表(但来源不同),它们都有:

  • 相同的列名和数据类型
  • 行数和列数相同
  • 每个单元格中的值相同
只需返回一个全新的表,该表的组成实际上与两个原始表完全相同

我错过了什么

两个相同的表(共享我上面提到的功能)是否可能具有其他不同的属性(用户看不见)

或者这是否可能实际上是一种不好的方法?可能的替代方案是什么

已编辑

  • 两个表具有相同的基本数据类型,例如:
    System.String
    System.Int32
    System.DateTime
  • 这些代码并不适用于我测试的所有示例
  • 这是一个1个样本的打印屏幕(使用
    DataSet
    Visualizer)

  • 我曾经写过类似的东西,这就是我使用的方法:

    首先,只有在每个表中没有重复行的情况下,这种方法才有效

    使用主键

    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());
        }
    }
    
    foreach(第二行中的数据行dr)
    {
    列表l=新列表();
    foreach(第二列中的数据列dc)l.Add(dr[dc]);
    if(First.Rows.Find(l.ToArray())==null)//未找到
    {
    table.Rows.Add(l.ToArray());
    }
    }
    foreach(第一行中的数据行dr)
    {
    列表l=新列表();
    foreach(第一列中的数据列dc)l.Add(dr[dc]);
    if(Second.Rows.Find(l.ToArray())==null)//未找到
    {
    table.Rows.Add(l.ToArray());
    }
    }
    

    干杯,

    这些列的数据类型是什么?它只是原语吗?也许它们包含一些引用不相等的对象<代码>(…)但具有不同的源代码(…)可能是此处的键。您应该知道,在以下内容之后(例如)
    var Bitmap=Bitmap.FromFile(@“C:\picture.jpg”);var bitmapB=Bitmap.FromFile(@“C:\picture.jpg”)
    -
    bitmappa==bitmappb
    false
    理论上该方法应该有效。在预期匹配不起作用的情况下,列的顺序是否相同?你能提供一个不起作用的小例子吗?明天我会的,等我回到办公室=)问题编辑完毕。请检查。我用原始数据类型和样本数据在内存中创建了相同的数据表,并使用了您的方法,结果是一个空的差异表。列或数据必须有特定的内容。我将尝试限制您在表中选择的列,并查看是否可以隔离导致问题的特定列。