Solved

How to merge two datatables and remove duplicate rows

Posted on 2014-09-19
4
4,131 Views
Last Modified: 2014-10-01
I have two datatables with identical columns, the uniqueness is based on three columns
ID, Code and BusDate

How can I remove the duplicates
dt1.Merge(dt2) does not remove the duplicates because there are a total of six columns
0
Comment
Question by:countrymeister
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40334254
Use an append query.

1.

Append all the rows in one table

2.

Now append all the rows in the second where there is NO match in those three columns (x != x1 and y != y1 and z != z1)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40334256
oops, use an OR not the and in the condition

Use an append query.
Append all the rows in one table
Now append all the rows in the second where there is NO match in those three columns (x != x1 or y != y1 or z != z1)
0
 
LVL 25

Expert Comment

by:apeter
ID: 40335651
Hope this helps.

var distinctItems = <yourDatamodelCollection>.Distinct();

To match on only some of the properties, create a custom equality comparer, e.g.:

class DistinctItemComparer : IEqualityComparer<Item> {

    public bool Equals(Item x, Item y) {
        return x.Id == y.Id &&
            x.Name == y.Name &&
            x.Code == y.Code &&
            x.Price == y.Price;
    }

    public int GetHashCode(Item obj) {
        return obj.Id.GetHashCode() ^
            obj.Name.GetHashCode() ^
            obj.Code.GetHashCode() ^
            obj.Price.GetHashCode();
    }
}
Then use it like this:

var distinctItems = <yourDatamodelCollection>.Distinct(new DistinctItemComparer());
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40336433
Set the PrimaryKey property of the DataTable objects. You can do that in the designer if you have a typed dataset, or do it in code:
dt1.PrimaryKey = new[] { dt1.Columns["ID"], dt1.Columns["Code"], dt1.Columns["BusDate"] };
dt2.PrimaryKey = new[] { dt2.Columns["ID"], dt2.Columns["Code"], dt2.Columns["BusDate"] };

Open in new window

You can then have dt2 rows overwrite rows from dt1 with the same keys:
dt1.Merge(dt2);

Open in new window

or have dt1 rows preserved:
dt1.Merge(dt2, true);

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question