Solved

How do I get the difference in 2 datatables

Posted on 2014-12-15
9
63 Views
Last Modified: 2016-02-18
I have 2 datatables,

Table A        Table B
1                   3
2                   4
3                   5
                     6

I want the results to be

Table C
1
2

I thought this was called a left outer join but statement is not giving me the correct results

               var results = (from rA in dtA.AsEnumerable()
                                                 join rB in dtB.AsEnumerable()
                                         on rA.Field<string>(PrimaryKeyName) equals rB.Field<string>(PrimaryKeyName) into joinedRows
                                                 from ab in joinedRows.DefaultIfEmpty()
                                                 select ab);
0
Comment
Question by:matthew phung
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40500975
SELECT a.Field
FROM TableA a
   LEFT JOIN TableB b ON a.Field = b.Field
WHERE b.Field IS NULL

or (SQL 2008 and beyond)

SELECT a.Field
FROM TableA
EXCEPT
SELECT b.Field
FROM TableB
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 40500976
I mean... the SQL query would be:

select
   *
from
   tableA as A
      left join tableB as B on B.<col> = A.<col>
where
   B.<col> is null;

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40500984
Test code in SSMS..
IF EXISTS (SELECT name FROM sys.tables WHERE name='a') 
	DROP TABLE a

IF EXISTS (SELECT name FROM sys.tables WHERE name='b') 
	DROP TABLE b

CREATE TABLE a (val int) 
INSERT INTO a (val) VALUES (1), (2), (3) 

CREATE TABLE b (val int) 
INSERT INTO b (val) VALUES (3), (4), (5), (6)  

SELECT a.val
FROM a
   LEFT JOIN b ON a.val = b.val 
WHERE b.val IS NULL

-- or (SQL 2008 and beyond) 

SELECT a.val
FROM a
EXCEPT
SELECT b.val
FROM b

Open in new window

0
 
LVL 1

Expert Comment

by:psdesignadmin
ID: 40501087
dtA.Merge(dtB ); //Here we marge the datatables
dtA.GetChanges();// Get Changes

Open in new window


Now we find the Distinct records of DataTable dtA
  dtC= dtA.DefaultView.ToTable(true, cols); 

Open in new window


DefaultView returns the Distinct record but before that we have to pass the datatable as a column Array (cols) to this method.

~Tim
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:matthew phung
ID: 40501219
I've requested that this question be deleted for the following reason:

My code was correct. My data was wrong.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40501220
Cancelling the delete request, as multiple experts answered the question as it was asked.

If your question did not accurately reflect requirements, then please accept answer(s) here, finalize your requirements, and ask them in a new question.
0
 
LVL 1

Expert Comment

by:psdesignadmin
ID: 40502528
Accept http:#a40500975 since it was the first to give a correct answer based on information given.

~Tim
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 40502726
^=== agree
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

12 Experts available now in Live!

Get 1:1 Help Now