Over a million developers have joined DZone.

How to Compare Two Data Tables Using Linq in Asp.net

DZone's Guide to

How to Compare Two Data Tables Using Linq in Asp.net

A code snippet to compare two data tables using Linq in Microsoft's .NET language.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

This article is used to get different records from datatable1 compared with datatable2 using LINQ Query.  This mismatched record gets from another data-table. 


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


Create flexible schemas using dynamic columns for semi-structured data. Learn how.

asp net

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}