按内容比较两个相同的数据表 [英] Compare Two Identical Datatable By Content

查看:83
本文介绍了按内容比较两个相同的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据表。它们完全相同。我想通过

cell'的内容来比较它们。它们都是一样的。


但是dt1 == dt2或

dt1.GetHashCode()== dt2.GetHashCode()不起作用。


theese数据表中有大量的行。所以我不想要

来枚举每一行。对于我的

当前应用程序来说,这是无效和不可接受的。


那么我如何比较这些数据表的内容呢?


让我详细描述一下:


我有一个数据表。 dtOld。

在表单加载事件中,我从数据库中填充此数据表,并且

将其绑定到网格。


用户可能会更改网格中的某些数据。然后按保存按钮。在这个

点,我可以感知哪一行改变了(.GetChanges)并将这一行更新

到数据库。


所以 - 这是我的问题开始:


在此更新操作之后,我从

数据库重新查询新表,名称为dtNew数据表。


我已经拥有一个绑定到网格的数据表。叫做dtOld。

我接受对dtOld的更改。


如果没有其他用户做出任何更改,dtOld和dtNew是相同的。


在调试模式下,我可以看到它们具有完全相同的数据。

例如它们都有10行。列中的相同数据。


但是当我比较它们时,c#表示它们不相等。从理论上讲,他们可能不等于参考。对。但我想用他们的

内容进行比较。


如何以有效的方式管理这个?


问候。

解决方案

您好

您可以在两个数据表之间执行virtuall联合操作。如果

没有。结果数据表中的行数是相同的,然后两个数据项

相同,否则结果数据表中的行数将增加。


这里是一个实现在我们创建的union函数中,它需要两个

数据表作为输入,并在两个数据表上执行union

之后返回一个带行的数据表。

public DataTable Union(DataTable First,DataTable Second)


{

//结果表

DataTable table = new DataTable( Union);

//构建新列

DataColumn [] newcolumns = new DataColumn [First.Columns.Count];

for(int i = 0; i< First.Columns.Count; i ++)

{

newcolumns [i] = new DataColumn(First.Columns [i]。 ColumnName,

First.Columns [i] .DataType);

}

//向结果表添加新列

table.Columns.AddRange(newcolumns);

table.BeginLoadData();

//从fi加载数据第一个表

foreach(First.Rows中的DataRow行)

{

table.LoadDataRow(row.ItemArray,true);

}

//从第二个表加载数据

foreach(Second.Rows中的DataRow行)

{

table.LoadDataRow(row.ItemArray,true);

}

table.EndLoadData();

返回表;

}


" inpuarg"写道:


我有2个数据表。它们完全相同。我想通过

cell'的内容来比较它们。它们都是一样的。


但是dt1 == dt2或

dt1.GetHashCode()== dt2.GetHashCode()不起作用。


theese数据表中有大量的行。所以我不想要

来枚举每一行。对于我的

当前应用程序来说,这是无效和不可接受的。


那么我如何比较这些数据表的内容呢?


让我详细描述一下:


我有一个数据表。 dtOld。

在表单加载事件中,我从数据库中填充此数据表,并且

将其绑定到网格。


用户可能会更改网格中的某些数据。然后按保存按钮。在这个

点,我可以感知哪一行改变了(.GetChanges)并将这一行更新

到数据库。


所以 - 这是我的问题开始:


在此更新操作之后,我从

数据库重新查询新表,名称为dtNew数据表。


我已经拥有一个绑定到网格的数据表。叫做dtOld。

我接受对dtOld的更改。


如果没有其他用户做出任何更改,dtOld和dtNew是相同的。


在调试模式下,我可以看到它们具有完全相同的数据。

例如它们都有10行。列中的相同数据。


但是当我比较它们时,c#表示它们不相等。从理论上讲,他们可能不等于参考。对。但我想用他们的

内容进行比较。


如何以有效的方式管理这个?


问候。


2006年12月18日星期一01:44:00 -0800,Ciaran O'''Donnell

< Ci ************ @ discussion.microsoft.comwrote:


>确保你需要通过每个在整个表格中的单元格,直到找到第一个区别。



这对我无效,因为

数据表中的行太多。


>您可以先将快捷方式放入,例如,对象.Equals检查它们是否实际上是同一个表,然后比较行计数和列数,比较模式(列数据类型)。您可以尝试随机抽样,但它并不能给您带来确定性。



我可以在调试窗口看到数据相同。但是c#说theese是不等于
。 Hashcodes也不一样。我认为这是关于.net的

对象比较模型。这里的原因是一个样本:

假设我有一个名为Person的类:


公共类人员

{

public string Name ="" ;;

}


让我测试==操作:

人a =新人();

a.Name =" Person1";


人b =新人();

v.Name =" Person1";


if(a == b)

{

MessageBox .Show(" Equal);

}

else

{

MessageBox.Show(" a:" + a.GetHashCode()+ Environment.NewLine +

" b:" + b.GetHashCode());

}

如果您运行此代码,您将看到theese对象不相等。

原因.net因为名称而没有比较这些对象

值(这是预期的结果 - 我不反对)

但是必须有一种覆盖人类的==操作的方法

并对Name的值进行比较。而且我知道这个

也存在。


所以我在这里问这对Datatable对象有用吗?

是有这样的方法吗?方式,workarround等。


感谢您的报价。但正如我之前提到的,对于数百万条记录而言,这不是一个有效的方法。否则如果我通过每行循环

我可以使用

cell1.ToString()== otherCell1.ToString()方法手动比较单元格。


但我会记住。谢谢。

星期一,2006年12月18日04:42:00 -0800,amit_mitra

< am ******* @ discussion.microsoft.comwrote :


>嗨

您可以在两个数据表之间执行virtuall联合操作。如果没有。结果数据表中的行是相同的,然后两个数据项相同,否则结果数据表中的行数将增加。

这里是我们创建的union函数的一个实现,它将两个
数据表作为输入,并在两个数据表上执行联合后返回带有行的数据表。

public DataTable Union(DataTable First,DataTable Second)

$

//结果表

DataTable table = new DataTable(" Union");

//构建新列

DataColumn [] newcolumns = new DataColumn [First.Columns.Count];

for(int i = 0; i< First.Columns.Count; i ++)

{

newcolumns [i] = new DataColumn(First.Columns [i] .ColumnName,
First.Columns [i] .DataType);

}

//向结果表添加新列

table.Columns.AddRange(newcolumns);

table.BeginLoadData( );

//从第一个表加载数据

foreach(First.Rows中的DataRow行)

{

table.LoadDataRow(row.ItemArray ,true);

}

//从第二个表加载数据

foreach(Second.Rows中的DataRow行)

{

table.LoadDataRow(row.ItemArray,true);

}

table.EndLoadData();

返回表;
}

" inpuarg"写道:


>我有2个数据表。它们完全相同。我想通过
cell的内容来比较它们。它们都是一样的。

但是dt1 == dt2或
dt1.GetHashCode()== dt2.GetHashCode()不起作用。

那里在theese数据表中有大量的行。所以我不想要枚举每一行。这对我目前的申请来说并不高效且不可接受。

那么如何比较这些数据表的内容?

让我详细描述一下:

我有一个数据表。 dtOld。
在表单加载事件中,我从数据库填充此数据表并将其绑定到网格。

用户可能会更改网格中的某些数据。然后按保存按钮。在这一点上,我可以感知哪一行改变了(.GetChanges)并将这一行更新到数据库。

所以 - 这是我的问题开始:
<在这个更新操作之后,我从
数据库重新查询新表,名称是dtNew数据表。

我已经有一个绑定到网格的数据表。叫dtOld。
我接受对dtOld的更改。

如果没有其他用户做出任何更改,dtOld和dtNew是相同的。

在调试模式下,我可以看到他们有完全相同的数据。
例如他们都有10行。列中的数据相同。

但是当我比较它们时,c#表示它们不相等。从理论上讲,它们可能不等于参考。对。但我希望通过他们的内容进行比较。

我如何以有效的方式管理这个?

问候。


I have 2 datatables. They are identical. I want to compare them by
cell''s content. They are all same.

But dt1 == dt2 or
dt1.GetHashCode() == dt2.GetHashCode() doesn ''t work.

There are big amount of rows in theese datatables . So i don ''t want
to enumerate each rows. This is not efficient and unacceptable for my
current application.

So how can i compare theese datatables by their contents?

Let me describe more in detail :

I have a datatable. dtOld.
In the form load event i am filling this datatable from database and
bound it to a grid.

User may change some data in the grid. Then press save button. At this
point, i can sense which row changed (.GetChanges) and update this row
to database.

So - here is my problem begins :

After this update operation i am requerying the new table from
database and name is as dtNew datatable.

And i already have a datatable which is bound to grid. called dtOld.
I am accepting changes on dtOld.

If no other user made any changes , dtOld and dtNew are same.

In debug mode i can see that they have exact same data.
for instance both of them have 10 rows. and same data in columns.

But when i compared them c# says they are not equal. In theory they
might not equal by reference . Right. But i want to compare by their
content.

How can i manage this in efficient way ?

Regards.

解决方案

Hi
You can perform a virtuall union operation between the two datatables. If
the no. of rows in the resulting datatable is same then both the databales
are same, otherwise the row count in the resulting datatable will increase.

here is one implementation of the union function we created , it takes two
datatables as input and returns a datatable with rows after performing union
on the two datables.
public DataTable Union (DataTable First, DataTable Second)

{
//Result table
DataTable table = new DataTable("Union");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.Count];
for(int i=0; i < First.Columns.Count; i++)
{
newcolumns[i] = new DataColumn(First.Columns[i].ColumnName,
First.Columns[i].DataType);
}
//add new columns to result table
table.Columns.AddRange(newcolumns);
table.BeginLoadData();
//Load data from first table
foreach(DataRow row in First.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
//Load data from second table
foreach(DataRow row in Second.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}


"inpuarg" wrote:

I have 2 datatables. They are identical. I want to compare them by
cell''s content. They are all same.

But dt1 == dt2 or
dt1.GetHashCode() == dt2.GetHashCode() doesn ''t work.

There are big amount of rows in theese datatables . So i don ''t want
to enumerate each rows. This is not efficient and unacceptable for my
current application.

So how can i compare theese datatables by their contents?

Let me describe more in detail :

I have a datatable. dtOld.
In the form load event i am filling this datatable from database and
bound it to a grid.

User may change some data in the grid. Then press save button. At this
point, i can sense which row changed (.GetChanges) and update this row
to database.

So - here is my problem begins :

After this update operation i am requerying the new table from
database and name is as dtNew datatable.

And i already have a datatable which is bound to grid. called dtOld.
I am accepting changes on dtOld.

If no other user made any changes , dtOld and dtNew are same.

In debug mode i can see that they have exact same data.
for instance both of them have 10 rows. and same data in columns.

But when i compared them c# says they are not equal. In theory they
might not equal by reference . Right. But i want to compare by their
content.

How can i manage this in efficient way ?

Regards.


On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''''Donnell
<Ci************@discussions.microsoft.comwrote:

>To be sure you would need to go through each cell in the entire table till
you find the first difference.

This is not effective for me cause there are too many rows in the
datatables.

>You could put shortcuts in first like,
object.Equals to check if they are actually the same table, then compare row
counts and column counts, compare the schema (column datatypes). You could
try random sampling but it doesnt give you certainty.

I can see in debug window that data are same. But c# says theese are
not equal. Hashcodes are not same also. I think this is about .net ''s
object compare model. Cause here is a sample :
Suppose that i have a class called Person :

public class Person
{
public string Name = "";
}

and let me test == operation :
Person a = new Person();
a.Name = "Person1";

Person b = new Person();
v.Name = "Person1";

if (a == b)
{
MessageBox.Show ("Equal");
}
else
{
MessageBox.Show("a : " + a.GetHashCode () + Environment.NewLine +
"b: " + b.GetHashCode() );
}
If you run this code you ''Ll see that theese objects are not equal.
Cause .net is not comparing theese objects due to their name ''s
values (which is expected result - i am not against)
But there must be a way of overriding == operation for Person class
and make this comparison over Name ''s values. And i know that this
exist too.

So here i am asking that is this possible for Datatable object ?
Is there such a method ? way , workarround etc.


thank you for your offer. But as i ''ve mentioned before this is not an
effective way for millions of records. Otherwise if i would loop
through each rows i can manually compare cells by using
cell1.ToString() == otherCell1.ToString() methods.

But i ''ll keep in my mind. Thank you.
On Mon, 18 Dec 2006 04:42:00 -0800, amit_mitra
<am*******@discussions.microsoft.comwrote:

>Hi
You can perform a virtuall union operation between the two datatables. If
the no. of rows in the resulting datatable is same then both the databales
are same, otherwise the row count in the resulting datatable will increase.

here is one implementation of the union function we created , it takes two
datatables as input and returns a datatable with rows after performing union
on the two datables.
public DataTable Union (DataTable First, DataTable Second)

{
//Result table
DataTable table = new DataTable("Union");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.Count];
for(int i=0; i < First.Columns.Count; i++)
{
newcolumns[i] = new DataColumn(First.Columns[i].ColumnName,
First.Columns[i].DataType);
}
//add new columns to result table
table.Columns.AddRange(newcolumns);
table.BeginLoadData();
//Load data from first table
foreach(DataRow row in First.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
//Load data from second table
foreach(DataRow row in Second.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}


"inpuarg" wrote:

>I have 2 datatables. They are identical. I want to compare them by
cell''s content. They are all same.

But dt1 == dt2 or
dt1.GetHashCode() == dt2.GetHashCode() doesn ''t work.

There are big amount of rows in theese datatables . So i don ''t want
to enumerate each rows. This is not efficient and unacceptable for my
current application.

So how can i compare theese datatables by their contents?

Let me describe more in detail :

I have a datatable. dtOld.
In the form load event i am filling this datatable from database and
bound it to a grid.

User may change some data in the grid. Then press save button. At this
point, i can sense which row changed (.GetChanges) and update this row
to database.

So - here is my problem begins :

After this update operation i am requerying the new table from
database and name is as dtNew datatable.

And i already have a datatable which is bound to grid. called dtOld.
I am accepting changes on dtOld.

If no other user made any changes , dtOld and dtNew are same.

In debug mode i can see that they have exact same data.
for instance both of them have 10 rows. and same data in columns.

But when i compared them c# says they are not equal. In theory they
might not equal by reference . Right. But i want to compare by their
content.

How can i manage this in efficient way ?

Regards.


这篇关于按内容比较两个相同的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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