Wednesday, February 17, 2016

How to compare two datatable using Linq in Asp.net


Compare two datatable having same datatype column using LINQ Query.
This article is used to get different records from datatable1 compared with datatable2 using LINQ Query.  This mismatched record get from another datatable. 


Query

var record1 = datatable1.AsEnumerable().Select(a => new {              
                  matchColumnValue = a["columnName"].ToString() });

var record2 = datatable2.AsEnumerable().Select(b => new {
                   matchColumnValue = b["columnName"].ToString() });

var exceptTotalRecord = record1.Except(record2);

DataTable datatableFinal = (from a in exceptDirectFare.AsEnumerable()
  select a).CopyToDataTable();