Click here to Skip to main content
16,015,973 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Compare two datatable using LINQ Query

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
12 Mar 2012CPOL 68.3K   6   5

Introduction

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. 

C++
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();

 

License

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


Written By
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
Rahul Vibhute23-Aug-14 7:02
Rahul Vibhute23-Aug-14 7:02 
QuestionAdditional information: The source contains no DataRows. Pin
Mlsoun14-Jan-14 17:44
Mlsoun14-Jan-14 17:44 
Questionhow to add dynamic columns instead of giving id Pin
ramprasad07@hotmail.com24-Jul-13 4:27
ramprasad07@hotmail.com24-Jul-13 4:27 
how to add dynamic columns instead of giving id
GeneralMy vote of 3 Pin
automanx4-Dec-12 1:22
automanx4-Dec-12 1:22 
Questionerror when two data tables have same data Pin
pinnintybalaji19-Nov-12 3:44
pinnintybalaji19-Nov-12 3:44 

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.