473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 35365
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.
Dec 18 '06 #2
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.

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.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.
Dec 18 '06 #4
Hic,

"inpuarg" <in*****@whereland.comwrote in message
news:c0********************************@4ax.com...
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.
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*******@discussions.microsoft.comwrote in message
news:1C**********************************@microsof t.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*****@whereland.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.ToString() 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*****@whereland.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.ToString() 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
(TransactionScope 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 "derivedclass"
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(IBaseClass baseClass)
{
if (baseClass == null) throw new ArgumentNullException();
this.baseClass = baseClass;
}
private string propertyC;
public string PropertyC
{
get { return propertyC; }
set { propertyC = value; }
}
public string PropertyA
{
get { return baseClass.PropertyA; }
set { baseClass.PropertyA = value; }
}
public string PropertyB
{
get { return baseClass.PropertyB; }
set { baseClass.PropertyB = value; }
}
}
Dec 19 '06 #10
(ignore; wrong topic)
Dec 19 '06 #11
On Tue, 19 Dec 2006 10:33:56 -0000, "Marc Gravell"
<ma**********@gmail.comwrote:
>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
(TransactionScope or ADO.Net transaction).
Assume that i have 5 rows when i 've load form.
1
2
3
4
5

user changed number 4. and i 've updated this info to database. by the
way - user 2 deleted the record number 2 and user three updated the
number four.

I want current user to see fresh data. that 's why i 'm checking.
>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...
data is too large.
>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
my application using both MSSQL2005 and Oracle and other databases
that supports stored procedure.
Dec 19 '06 #12

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

Similar topics

1
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...
2
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,...
2
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...
3
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...
3
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
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...
4
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...
3
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 |...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.