比较2个数据表以查找列之间的差异/准确性 [英] Compare 2 Datatables to find difference/accuracy between the columns

查看:72
本文介绍了比较2个数据表以查找列之间的差异/准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有2个单独的数据表,它们看起来非常相似,但是它们的行中的值例如可能不同.

So, I have 2 separate datatables, that look pretty identical but the values in their rows might be different for instance.

我可以通过创建一个临时标识列来获得唯一ID,如果可以的话,该列可以用作主键.因此,将ID列视为主键.

I can have an unique ID BY creating a temporary identity column that can be used as primary key if that will make it easier. so think of ID column as the primary key than.

表A

ID |  Name | Value1 | Value2 | Value3
-------------------------------------
1  |  Bob  |   50   |  150   |  35
2  |  Bill |   55   |  47    |  98
3  |  Pat  |   10   |  15    |  45
4  |  Cat  |   70   |  150   |  35

表B

ID |  Name | Value1 | Value2 | Value3
-------------------------------------
1  |  Bob  |   30   |  34    |  67
2  |  Bill |   55   |  47    |  98
3  |  Pat  |   100  |  15    |  45
4  |  Cat  |   70   |  100   |  20

输出应为:

表C

ID |  Name | TableAValue1 | TableBValue1 | DiffValue1 ....Samething for Value2 .....samething for value3
------------------------------------------------------
1  |  Bob  |   50         |   30         |    20          
2  |  Bill |   55         |   55         |    0               
3  |  Pat  |   10         |   100        |    90                
4  |  Cat  |   70         |   70         |    0                    

我知道执行此操作的乏味方法是使用forloop并遍历每一行,将各列行相互比较.但是我不确定如何用我想要的结果创建一个新的表C.另外,我认为使用Linq可能会有一个更简单的解决方案,对此我不太熟悉,但是如果linq的解决方案速度更快且代码行少,我会对这种解决方案感兴趣. 我正在寻找解决此问题的最佳方法.由于这些数据表的大小可以在5,000至15,000+行之间,因此内存使用成为一个问题.

I Know the tedious method to do this is by using a forloop and looping through each row comparing column rows with each other. But I am not sure how to create a new Table C with the results I want. Also I think there might be a simpler solution using Linq which I am not very familiar with but I would be interested in the solution with linq if it faster and less lines of code. I am looking for the most optimal/efficient way of going about this. as these datatables can be anywhere between 5,000 to 15,000+ rows in size so memory usage becomes an issue.

推荐答案

var tableC = new DataTable();
tableC.Columns.Add(new DataColumn("ID"));
tableC.Columns.Add(new DataColumn("Name"));
tableC.Columns.Add(new DataColumn("TableAValue1"));
tableC.Columns.Add(new DataColumn("TableBValue1"));
tableC.Columns.Add(new DataColumn("DiffValue1"));
foreach (DataRow rowA in tableA.Rows)
{
    foreach (DataRow rowB in tableB.Rows)
    {
        if (Convert.ToInt32(rowA["ID"]) == Convert.ToInt32(rowB["ID"]) &&
            rowA["Name"].ToString() == rowB["Name"].ToString() &&
            Convert.ToInt32(rowA["Value1"]) != Convert.ToInt32(rowB["Value1"]))
        {
            var newRow = tableC.NewRow();
            newRow["ID"] = rowA["ID"];
            newRow["Name"] = rowA["Name"];
            newRow["TableAValue1"] = rowA["Value1"];
            newRow["TableBValue1"] = rowB["Value1"];
            newRow["DiffValue1"] = Convert.ToInt32(rowA["Value1"]) - Convert.ToInt32(rowB["Value1"]);
            tableC.Rows.Add(newRow);
        }
    }
}

这篇关于比较2个数据表以查找列之间的差异/准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆