473,695 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare Two Identical Datatable By Content

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.
Dec 18 '06 #1
11 35400
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("Unio n");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.C ount];
for(int i=0; i < First.Columns.C ount; i++)
{
newcolumns[i] = new DataColumn(Firs t.Columns[i].ColumnName,
First.Columns[i].DataType);
}
//add new columns to result table
table.Columns.A ddRange(newcolu mns);
table.BeginLoad Data();
//Load data from first table
foreach(DataRow row in First.Rows)
{
table.LoadDataR ow(row.ItemArra y,true);
}
//Load data from second table
foreach(DataRow row in Second.Rows)
{
table.LoadDataR ow(row.ItemArra y,true);
}
table.EndLoadDa ta();
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.
Dec 18 '06 #2
On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''Donnell
<Ci************ @discussions.mi crosoft.comwrot e:
>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.Equal s 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.New Line +
"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.

Dec 18 '06 #3
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.ToSt ring() methods.

But i 'll keep in my mind. Thank you.
On Mon, 18 Dec 2006 04:42:00 -0800, amit_mitra
<am*******@disc ussions.microso ft.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("Unio n");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.C ount];
for(int i=0; i < First.Columns.C ount; i++)
{
newcolumns[i] = new DataColumn(Firs t.Columns[i].ColumnName,
First.Column s[i].DataType);
}
//add new columns to result table
table.Columns.A ddRange(newcolu mns);
table.BeginLoad Data();
//Load data from first table
foreach(DataRow row in First.Rows)
{
table.LoadDataR ow(row.ItemArra y,true);
}
//Load data from second table
foreach(DataRow row in Second.Rows)
{
table.LoadDataR ow(row.ItemArra y,true);
}
table.EndLoadDa ta();
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.GetHashCod e() == 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.
Dec 18 '06 #4
Hic,

"inpuarg" <in*****@wherel and.comwrote in message
news:c0******** *************** *********@4ax.c om...
On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''Donnell
<Ci************ @discussions.mi crosoft.comwrot e:
>>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.
Then how do you expect to know if there is a difference or not?

Iterate is the only way, and believe even if you do not do it and find a
framework method that does it for you at the end somebody will have to
iterate in ALL the rows and compare the values of the columns.

--
Ignacio Machin
machin AT laceupsolutions com
Dec 18 '06 #5
Hi,

"amit_mitra " <am*******@disc ussions.microso ft.comwrote in message
news:1C******** *************** ***********@mic rosoft.com...
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.
If I read the OP correctly he wants to compare the values of ALL the columns
of all the rows. The only way of doing it is by iterating and comparing
Dec 18 '06 #6
Hi,
"inpuarg" <in*****@wherel and.comwrote in message
news:7d******** *************** *********@4ax.c om...
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.ToSt ring() methods.
Can you provide more details of what you are trying to do? I mean where the
data comes from, that it consist of., etc
--
Ignacio Machin
machin AT laceupsolutions com
Dec 18 '06 #7
I have a datatable on a windows forms called newDt.
I am filling data from the database at form_load event.
Then i 'm binding theese datatable to a datagrid.

User manipulate data in a multi user environment. After user clicked
save button, i am updating changed rows to database. At this time i
have a datatable which is bounded to grid called oldDt. and i requery
database and have a new datatable called newDt.

i want to compare this two identical datatables by their contents.
cause it is possible that when user1 manipulating data on a grid,
another user might delete a row or add a row.
If i can compare theese two datatables :
i 'Ll check that if they are same - then this means no any other user
changed the database table. so i don 't need to refresh grid. If
theese are not same , if there are modified rows , added rows or
deleted rows at newDt , then i 'Ll refresh grid.
On Mon, 18 Dec 2006 11:14:47 -0500, "Ignacio Machin \( .NET/ C# MVP
\)" <machin TA laceupsolutions .comwrote:
>Hi,
"inpuarg" <in*****@wherel and.comwrote in message
news:7d******* *************** **********@4ax. com...
>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.ToSt ring() methods.

Can you provide more details of what you are trying to do? I mean where the
data comes from, that it consist of., etc
Dec 19 '06 #8
Why do it this way? Personally, I'd just look for the records that the
user has actively changed, and attempt to save them to the database,
letting the database tell me about concurrency issues (for instance,
by verifying a "timestamp" column at hte point of save). Otherwise, to
ensure that no users change things behind your back you would have to
lock all of the data while checking, which doesn't seem very
efficient. Of course, locking the rows in question is a good idea
(TransactionSco pe or ADO.Net transaction). Also, by the time you have
fetched all the data from the database, updating the UI is the least
of your issues, so if you have the data, just update it. But if the
data is large, you shouldn't really attempt to do it this way
anyway...

There are also (in 2.0) database-oriented change notifications (works
best with SqlServer 2005), but I'm not a big fan of these... not sure
about the scalability... maybe I'm prejudiced though...

Marc
Dec 19 '06 #9
Example using your names... note that "baseclass" and "derivedcla ss"
are a little vaguewhen removed from inhetance, but I thought it would
be clearer to keep the same for reference purposes. Note also that the
base (either BaseClass or IBaseClass) could be made cloneable
(ICloneable) if you needed to make copies of just the base section.

Marc

using System;
class Program
{
static void Main()
{
DerivedClass dc = new DerivedClass();
dc.PropertyA = "A";
dc.PropertyB = "B";
dc.PropertyC = "C";
IBaseClass bc = dc.BaseClass;
}
}
public interface IBaseClass
{
string PropertyA { get;set;}
string PropertyB {get;set;}
}
public class BaseClass : IBaseClass
{
private string propertyA;
public string PropertyA
{
get { return propertyA; }
set { propertyA = value; }
}
private string propertyB;
public string PropertyB
{
get { return propertyB; }
set { propertyB = value; }
}
}

public class DerivedClass : IBaseClass
{
public IBaseClass BaseClass { get { return baseClass; } }
private readonly IBaseClass baseClass;
public DerivedClass() : this(new BaseClass()) { }
public DerivedClass(IB aseClass baseClass)
{
if (baseClass == null) throw new ArgumentNullExc eption();
this.baseClass = baseClass;
}
private string propertyC;
public string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
public string PropertyA
{
get { return baseClass.Prope rtyA; }
set { baseClass.Prope rtyA = value; }
}
public string PropertyB
{
get { return baseClass.Prope rtyB; }
set { baseClass.Prope rtyB = value; }
}
}
Dec 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1713
by: Nedu N | last post by:
Hi All, I am facing problem in copying content of table from a untyped dataset into to a table inside the typed dataset. I wanted to copy the data into typed dataset in order to ease the further processing using typed references. DataSet ds = getDataFromDB(); - i am getting the dataset ds from a webservice I have got MyXSD which decsribes the shcema for the table which i am getting from the webservice
2
2743
by: Eunice | last post by:
Hello. I want to display the content of 'displayTable' object reversely. When from r=0 to r= displayTable.Length-1, the code works well to display. But when from r= displayTable.Length-1 to r=0, the code doesn't work to display. Thanks a lot for any help. **********
2
1837
by: Nedu N | last post by:
Hi Techies, I am facing problem in copying content of table from a untyped dataset into to a table inside the typed dataset. I wanted to copy the data into typed dataset in order to ease the further processing using typed references. DataSet ds = getDataFromDB(); - i am getting the dataset ds from a webservice I have got MyXSD which decsribes the shcema for the table which i am getting
3
2088
by: grey | last post by:
does anyone suggest me how to write a windows application for comparing two pdf content. The requirement is very easy... i only need to inform user two pdf are differnet, no need to spot where the difference.
3
3230
by: ago | last post by:
Hi, Is there any way to compare two identical table structures in access for different values in them. EG: Table 1: Name occupation rob plumber Table 2: Name occupation
5
5894
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to store a DataTable in any other way outside of our servers. In doing so, we leave ourselves open to large memory requirements. Furthermore, most web pages do not really support multiple changes per transaction. In other words, when the user submits...
4
3018
by: ndoe | last post by:
how to compare string in file i mean compare that string content digit or alphabet,and 1 question more can we count that line if first line variable = 1,if read second line variable = 2 and so on thanks e.g i have file with name data.txt with file like this 123456
3
3806
by: madankarmukta | last post by:
Hi, Objectives is to write the DataTable content to the text file in the format Col1 | Col2 | Col3 ------|-------|--------- Val11 | Val21 | Val31 Val12 | Val22 | Val32 Val13 | Val23 | Val33
3
2194
by: thaopham215 | last post by:
I have a datatable with the ordering of its DataClassIndex column before editting: DataclassIndex 0 1 2 3 4 When I start modifying some other cells in this datatable, the ordering of DataClassIndex column changes to something like this DataclassIndex 3 4 2 1 0 How can I modify a datatable content without altering the original order of the datatable.
0
8565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4339
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.