问题描述
我有2个数据表。它们完全相同。我想通过
cell'的内容来比较它们。它们都是一样的。
但是dt1 == dt2或
dt1.GetHashCode()== dt2.GetHashCode()不起作用。
theese数据表中有大量的行。所以我不想要
来枚举每一行。对于我的
当前应用程序来说,这是无效和不可接受的。
那么我如何比较这些数据表的内容呢?
让我详细描述一下:
我有一个数据表。 dtOld。
在表单加载事件中,我从数据库中填充此数据表,并且
将其绑定到网格。
用户可能会更改网格中的某些数据。然后按保存按钮。在这个
点,我可以感知哪一行改变了(.GetChanges)并将这一行更新
到数据库。
所以 - 这是我的问题开始:
在此更新操作之后,我从
数据库重新查询新表,名称为dtNew数据表。
我已经拥有一个绑定到网格的数据表。叫做dtOld。
我接受对dtOld的更改。
如果没有其他用户做出任何更改,dtOld和dtNew是相同的。
在调试模式下,我可以看到它们具有完全相同的数据。
例如它们都有10行。列中的相同数据。
但是当我比较它们时,c#表示它们不相等。从理论上讲,他们可能不等于参考。对。但我想用他们的
内容进行比较。
如何以有效的方式管理这个?
问候。
这对我无效,因为
数据表中的行太多。
我可以在调试窗口看到数据相同。但是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等。
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.
This is not effective for me cause there are too many rows in the
datatables.
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.
这篇关于按内容比较两个相同的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!