How to compare two datatable remove duplicate using Linq

Hello, I need to compare two data table and in that, we have to compare the name coloumn
Example Dt1:
‘Name’ — ‘city’
Chethan — Bangalore
Sandy ----- Bangalore

‘Name’ ----- ‘city’
Guru ------- Hassan
Chethan ---- Bangalore

‘Name’---- ‘City’
Chethan— Banglore

How to compare like this using LINQ

I use this LINQ

DataTable1.AsEnumerable.Intersect(DataTable2.AsEnumerable, system.Data.DataRowComparer.Default).CopyToDataTable

Can any one explain this system.Data.DataRowComparer.Default
and this .Intersect

Or if you have any other way to write this code please replay to this post

Chethan P
Happy Automation

Hi @copy_writes

Use below linq query to compare all columns

dt1.AsEnumerable().Where(function(row) dt2.AsEnumerable().Any(function(x) x.Equals( row) )).CopyToDataTable

for specific column use below

dt1.AsEnumerable().Where(function(row) dt2.AsEnumerable().Any(function(x) x(“Name”).tostring.Equals( row(“Name”).tostring) )).CopyToDataTable

Note: If this code resolve your problem please mark as a solution

Amaresan. P

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.