Friday, April 26, 2013

How To- Find Duplicate Records in DataTable Using LINQ

This is a simple tip post . Here, I am explaining how to find the duplicate records in DataTable using LINQ.

In my previous posts, I explained Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#Hard drive information using C#Create Directory/Folder using C#Check Internet Connection using C#SQL Server Database BackUp using C# and some other articles related to C#ASP.Net jQuery, Java Script and SQL Server.

Query for finding duplicate records in  DataTable using LINQ is very simple. For example, We have the following DataTable.

Data Table
Here you can see that "Name" and "Age" columns have some duplicate records.Now we will find these duplicate records using LINQ.

 

Find duplicate records based on single column

If you want to find the  duplicate records from the above table based on single column i.e "Name" then you will have to write the following LINQ query
var duplicates = dt.AsEnumerable()
                 .GroupBy(r =>  r[0])//Using Column Index
                 .Where(gr => gr.Count() > 1)
                 .Select(g => g.Key);
foreach (var d in duplicates)
     Console.WriteLine(d);
Console.ReadKey();

OR

var duplicates = dt.AsEnumerable()
                 .GroupBy(r =>  r["Name"])//Using Column Name
                 .Where(gr => gr.Count() > 1)
                 .Select(g => g.Key);
foreach (var d in duplicates)
     Console.WriteLine(d);
Console.ReadKey();
Output of the above query will be-
Duplicate Record in DataTable using LINQ

 

Find duplicate records based on multiple column

Following query will give you duplicate records in DataTable based on mulitiple columns.
var duplicates = dt.AsEnumerable()
                 .GroupBy(r => new { Name = r[0], Age = r[1] })//Using Column Index
                 .Where(gr => gr.Count() > 1)
                 .Select(g => g.Key);
 foreach (var d in duplicates)
      Console.WriteLine(d);
Console.ReadKey();

OR

var duplicates = dt.AsEnumerable()
                 .GroupBy(r => new { Name = r[0], Age = r[1] })//Using Column Name
                 .Where(gr => gr.Count() > 1)
                 .Select(g => g.Key);
foreach (var d in duplicates)
     Console.WriteLine(d);
Console.ReadKey();
Output of the above query will be-
Duplicate Record in DataTable using LINQ
I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo