VB.Net - Comparing Two DataTables

Good Day Experts!

I have 2 lists of AccountNumbers.  I am looking for duplicate product numbers across all the accounts.  Here are my 2 lists and a little summary of what I am doing:

1     1
2     2
4     4    
5     5
6     6

I loop through first list comparing to all in the second list.  Problem I have is...say I find a duplicate in Account 1 and 2...then I also get the same record when on Account 2 and comparing to Account 1.  

Do you have any ideas on hos I can better do this compare to eliminate the extra records?

Who is Participating?

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

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.

In your example, you don't really need a second scan to compare the second list to the first because it will produce the same result as the first scan.

But for example purposes, I can add a flag for each duplicate I find in the first scan.  For example, when you loop through the first list comparing to the second list I would associate a flag for each duplicate and when comparing second list to first it can ignore all the ones that are flagged as duplicate already.

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
Fernando SotoRetiredCommented:
Hi Jimbo99999;

If I understand you correctly you want the unique account number from both list without any duplicates. If that is the case and you have two list of, lets say integers for account numbers, then using Union set operations will work for this.

The following Union operation on two sequences of integers will return a sequence contains the unique elements from both input sequences.

'' Using the test data from the question
Dim acctList1 As New List(Of Integer) From { 1, 2, 3, 4 , 5, 6 }
Dim acctList2 As New List(Of Integer) From { 1, 2, 3, 4 , 5, 6 }

'' The variable uniqueAcctVals will contain a the following List(Of Integer) 1, 2, 3, 4, 5, 6
Dim uniqueAcctVals = acctList1.Union(acctList2)

'' Test data using different values 
Dim accts1 As New List(Of Integer) From { 1, 2, 3, 4 , 5, 6 }
Dim accts2 As New List(Of Integer) From { 6, 3, 9, 7 , 12, 4 }

'' The variable uniqueAcctVals2 will return a List(Of Integer) 1, 2, 3, 4, 5, 6, 9, 7, 12
Dim uniqueAcctVals2 = accts1.Union(accts2)

Open in new window

Jimbo99999Author Commented:
Thank you all for the replies.  I will go the route Wayne88 alluded to.  Fernando Soto, I have never used your idea but I appreciate your contribution and will put it in my toolbox for future use.

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
Visual Basic.NET

From novice to tech pro — start learning today.