Click here to Skip to main content
13,251,760 members (25,829 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 12 Mar 2012

Compare two datatable using LINQ Query

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.


Compare two datatable having same datatype column using LINQ Query

Using the code

This tips are used to get Mismatched records from datatable1 compared with datatable2 using LINQ Query.  This mismatched records get from another datatable. 

var qry1 = datatable1.AsEnumerable().Select(a => new { MobileNo = a["ID"].ToString() });
var qry2 = datatable2.AsEnumerable().Select(b => new { MobileNo = b["ID"].ToString() });

var exceptAB = qry1.Except(qry2);

DataTable dtMisMatch = (from a in datatable1.AsEnumerable()
                           join ab in exceptAB on a["ID"].ToString() equals ab.MobileNo
                           select a).CopyToDataTable();



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ramanujam Shankar
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThanks Pin
Member 1070007823-Aug-14 8:02
memberMember 1070007823-Aug-14 8:02 
QuestionAdditional information: The source contains no DataRows. Pin
Mlsoun14-Jan-14 18:44
memberMlsoun14-Jan-14 18:44 
Questionhow to add dynamic columns instead of giving id Pin
ramprasad07@hotmail.com24-Jul-13 5:27
memberramprasad07@hotmail.com24-Jul-13 5:27 
GeneralMy vote of 3 Pin
automanx4-Dec-12 2:22
memberautomanx4-Dec-12 2:22 
Questionerror when two data tables have same data Pin
pinnintybalaji19-Nov-12 4:44
memberpinnintybalaji19-Nov-12 4:44 
Hi ,

I have seen u r code its good and I am using this ,but I am getting error when two data tables have same data and remove then it must return null ,but i was throwing to exception .

Can u give any idea what to do ,please help me on this.

Thank you

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 12 Mar 2012
Article Copyright 2012 by Ramanujam Shankar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid