Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

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

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


  


MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

Topics:
asp net

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}