locked
C# find duplicate rows between two Datatables RRS feed

  • Question

  • User-1118822443 posted

    I have Dt1----> 32432 rows

    Dt2 ------>24239 rows. Have column name Account in both tables

    var duplicateRecords = (from r1 in Dt1.AsEnumerable()
    join r2 in Dt2 .AsEnumerable() on r1.Field<string>("Account ") equals r2.Field<string>("Account ")
    select r2).ToList();

    Surprisingly duplicateRecords returns 24247 rows which are more than no of rows in Dt2. How is it even possible? What is going wrong here

    When i try to delete duplicate records i am getting The given Datarow is not in the current DataRowCollection exception.

    duplicateRecords.ForEach(r=>Dt2.Rows.Remove(r)) --->exception

    Friday, August 10, 2018 9:16 AM

Answers

  • User465171450 posted

    The number could be larger if there are duplicates in Dt1. So if there are 2 records in DT1 that are duplicated and there are two rows in DT2 that have the same value you end up with both sets of matches, which end up with 4  as each row in DT1 that is a duplicate matches to all the duplicates in Dt2.

    In the case where duplicates in Dt1 occur you can have duplicates in your duplicate list. So it can easily be that the first one removes and the second one explodes. You could try throwing a Distinct() prior to the .ToList() to ensure your duplicate records doesn't have duplicate records.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2018 12:17 PM