How to find a deleted row from two DataTables in C#

I have two DataTables as follows.
The 3rd row is removed from the Table 1.
I would like to do a diff of the two tables, and get an answer to tell me that the row had been deleted.
I tried with the following code but it doesn't work. The result it gave me was 11 rows unchanged.

Any idea please. Thanks.

            table1.AcceptChanges();
            table2.AcceptChanges();
            table1.Merge(table2, true);

            if(added != null)
                Debug.WriteLine("added = " + added.Rows.Count);
            if (deleted != null)
                Debug.WriteLine("deleted = " + deleted.Rows.Count);
            if (detached != null)
                Debug.WriteLine("detached = " + detached.Rows.Count);
            if (modified != null)
                Debug.WriteLine("modified = " + modified.Rows.Count);
            if (unchanged != null)
                Debug.WriteLine("unchanged = " + unchanged.Rows.Count);

Open in new window

 Table1
          Key1                Key2                 Key3                Key4                Key5               Key6               Key7
    2063620735   2107319875   1418390997    270727583    270727583    652640566    369246503
    1630856750    515371590    416266794   2014872019   2014872019    532186611    504554590
     971040548   2107319875   1418390997   1037345008   1037345008    652640566    369246503  <-- Deleted
    1604012077   2107319875    416266794   2014872019   2014872019    532186611    369246503
    1604012077    515371590   1418390997    270727583   1037345008    652640566    369246503
    2063620735    515371590    416266794    421288522   2014872019    532186611    369246503
 
  Table2
          Key1                Key2                 Key3                Key4                Key5               Key6               Key7
    2063620735   2107319875   1418390997    270727583    270727583    652640566    369246503
    1630856750    515371590    416266794   2014872019   2014872019    532186611    504554590
    1604012077   2107319875    416266794   2014872019   2014872019    532186611    369246503
    1604012077    515371590   1418390997    270727583   1037345008    652640566    369246503
    2063620735    515371590    416266794    421288522   2014872019    532186611    369246503
dominicwongAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Naitik GamitSoftware DeveloperCommented:
You can try linq to compare two datatables for example :
var removed_keys = Table1.AsEnumerable().Select(r => r.Field<int>("key1"))
        .Except(Table2.AsEnumerable().Select(r => r.Field<int>("key1")));

Open in new window


After getting removed keys select datatable
DataTable Table3 = (from row in Table1.AsEnumerable()
                   join key1 in removed_keys
                   on row.Field<int>("key1") equals key1
                   select row).CopyToDataTable();

Open in new window

0
dominicwongAuthor Commented:
Thanks Naitik.
As to your suggestion to find the the "removed_keys" by using only "key1", could I make use of all the columns in the table?
My example might be a bad one. For example, I might not know in advance which key is different.

Also I would like to be able to tell whether the 2nd table had added, deleted, modified, etc rows.
0
Naitik GamitSoftware DeveloperCommented:
Hi dominicwong You Can Try As :
DataTable Table3 = Table1.AsEnumerable().Except(
Table2.AsEnumerable(), DataRowComparer.Default).
CopyToDataTable();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dominicwongAuthor Commented:
Thanks Naitik. It works.
0
frankhelkCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Naitik Gamit (https:#a42081170)
-- Naitik Gamit (https:#a42079571)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.