Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Retrieve duplicate records from data table using LINQ

 Following code examples explains how to How to find duplicate record using Linq from DataTable.

Find duplicate record for single columns from Data Table using LINQ

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
            var duplicatesData = EmployeeData.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1);
//Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Retrieve duplicates for two columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records applying grouping on two columns.

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Get duplicate records for multiple columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records, applying grouping on three columns

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId"),
                    City= x.Field<string>("City")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }