Solved

How to merge two datatables and remove duplicate rows

Posted on 2014-09-19
4
4,468 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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