Click here to Skip to main content
14,693,117 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have a datatable dt which has name and subject as 2 of its columns. I want to find if there is any duplicate entries for the combination of these two column values .
Posted
Comments
ChintanShukla 26-Sep-14 5:30am
   
Duplicate Columns with same column name or Duplicate records(rows) within those columns?

DataTable distinct = dt.DefaultView.ToTable(true, "Subject, Name");
if (distinct.rows.count == dt.rows.count)
{
//there are no duplicates
}else {
// there are duplicates
}


This code creates new datatable object from existing one, but only creates distinct rows. If the counts match, you didn't have any duplicates.

ToTable true parameter means take distinct and the string is a list of column names that are basis for distinct comparison

If this helps, please take time to accept the solution so others may find it. Thank you.
   
v3
Comments
Upniwesh 28-Mar-17 15:28pm
   
Thank,

It has been solved my problem
try like below
class Program
   {
       static void Main(string[] args)
       {

           var duplicates = GetTable().AsEnumerable().GroupBy(i => new{Name = i.Field<string>("Name"),Subject = i.Field<string>("Subject")}).Where(g => g.Count() > 1).Select(g => new {g.Key.Name, g.Key.Subject}).ToList();
       }
       static DataTable GetTable()
       {
           var table = new DataTable();
           table.Columns.Add("Name", typeof(string));
           table.Columns.Add("Subject", typeof(string));
           table.Rows.Add("Jameel", "Maths");
           table.Rows.Add("Jameel", "Maths");
           table.Rows.Add("Arun", "Chemistry");
           return table;
       }
   }

Hope this helps
   
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900