PC Review


Reply
Thread Tools Rate Thread

Compare Two Identical Datatable By Content

 
 
inpuarg
Guest
Posts: n/a
 
      18th Dec 2006
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.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?YW1pdF9taXRyYQ==?=
Guest
Posts: n/a
 
      18th Dec 2006
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.
>

 
Reply With Quote
 
inpuarg
Guest
Posts: n/a
 
      18th Dec 2006
On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''Donnell
<(E-Mail Removed)> wrote:

>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.

 
Reply With Quote
 
inpuarg
Guest
Posts: n/a
 
      18th Dec 2006
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
<(E-Mail Removed)> wrote:

>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.
>>

 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      18th Dec 2006
Hic,

"inpuarg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''Donnell
> <(E-Mail Removed)> wrote:
>
>>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


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      18th Dec 2006
Hi,

"amit_mitra" <(E-Mail Removed)> wrote in message
news:1C29A53C-96A1-41F1-845C-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      18th Dec 2006
Hi,


"inpuarg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
inpuarg
Guest
Posts: n/a
 
      19th Dec 2006
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.com> wrote:

>Hi,
>
>
>"inpuarg" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      19th Dec 2006
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


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      19th Dec 2006
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; }
}
}


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I compare two worksheets to see if they're identical? sdg105 Microsoft Excel Misc 5 26th Sep 2008 12:57 PM
Way to compare two identical workbooks automatically? Jean-Francois Gauthier Microsoft Excel Programming 0 29th Feb 2008 04:52 PM
How to compare two identical tables in VB.NET + MS Access himanshupundir@gmail.com Microsoft VB .NET 4 12th Jul 2006 10:04 PM
COMPARE IDENTICAL DATA =?Utf-8?B?aG9sYWdpZ2k=?= Microsoft Excel New Users 3 27th Jun 2006 08:21 PM
Compare cells for identical values red_debs Microsoft Excel Misc 1 27th Sep 2004 08:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:41 PM.