Solved

How to merge two datatables and remove duplicate rows

Posted on 2014-09-19
4
3,811 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now