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

Discover Tarantool's unique features which include powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU core!

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


  


Discover Tarantool's unique features such as powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU.

Topics:
asp net

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}