Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Compare two datatable using LINQ Query

4.33/5 (2 votes)
12 Mar 2012CPOL 68.4K  

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)