Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have data table in c# and it contains more columns and rows I need to find the duplicate row and show the row number. Data table always comes with undefined column names it’s not fixed columns.Anyone please help me on this ?

C#
string CSVFilePathName = @"C:\Application Details\HH- Duplicate Rows work\Contains Duplicate Row.csv";

DataTable dt = ConvertCSVtoDataTable(CSVFilePathName);
if (dt.Rows.Count != DT1.Rows.Count)
{
    DataTable dt_UnCommonRows = new DataTable();
    if(dt.Rows.Count > 0 && DT1.Rows.Count > 0)
    {
        var DTS = dt.AsEnumerable().Except(DT1.AsEnumerable(), System.Data.DataRowComparer.Default);
dt_UnCommonRows = DTS.CopyToDataTable();
    }


What I have tried:

I tried with Linq but getting the error.

Error:The source contains no DataRows.

C#
Dt =dr.AsEnumerable().Except(DT1.AsEnumerable(),System.Data.DataRow compared.Default).CopyTiDataTable();
Posted
Updated 11-Jun-24 6:32am
v3
Comments
Jo_vb.net 11-Jun-24 11:22am    
We cannot see the source code which causes the error, so don't expect too much feedback.
prabhug100 11-Jun-24 11:48am    
string CSVFilePathName = @"C:\Application Details\HH- Duplicate Rows work\Contains Duplicate Row.csv";



DataTable dt = ConvertCSVtoDataTable(CSVFilePathName);
if (dt.Rows.Count != DT1.Rows.Count)

{

DataTable dt_UnCommonRows = new DataTable();

if(dt.Rows.Count > 0 && DT1.Rows.Count > 0)

{



var DTS = dt.AsEnumerable().Except(DT1.AsEnumerable(), System.Data.DataRowComparer.Default);

dt_UnCommonRows = DTS.CopyToDataTable();

}



1 solution

The easiest way to do this is not to compare the rows directly but to generate a hash code (MD5 is both simple to produce and complex enough to work nicely) for the content of each row, and then find identical hash values. (For absolute certainty, you can then compare those rows directly to be sure it's not a hash collision though that's unlikely in the phase space of MD5 unless you are handling huge numbers of rows).
 
Share this answer
 

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