Delete rows from a data table that exists in another data table

anish

I have a DataTable dt1 with columns C1(PK), C2, and C3(Unique Value) in a DataSet, on which after deleting some rows I will be doing AcceptChanges. I have another DataTable dt2 which is identical in structure to dtA. I would like to delete rows in dtA which exists in dtB compared in terms of C3(unique value) and do the AcceptChanges.

I could achieve very close, but not what I want by doing Except like below (learned from here):

var a = dt1.AsEnumerable().Select(r => r.Field<Guid>("C3"));
var b = dt2.AsEnumerable().Select(r => r.Field<Guid>("C3"));
var c = a.Except(b);

Problem with this approach is, I am getting only a list of values that belong in C3 column. I could do some more as shown here, but then again I will have a new DataTable, I wont still change the existing dt1 table.

Second approach I thought of was doing something like below:

foreach (DataRow r1 in ds1.Tables[0].Rows)
{
    foreach(DataRow r2 in dt2.Rows)
    {
        if (r1.Field<Guid>("Guid1") == r2.Field<Guid>("Guid2"))
        {
            r1.Delete();
        }
    }
}

Here as soon as row is deleted, I get an error saying "This row has been removed from a table and does not have any data." What am I missing here?

I also have a feeling that there might be a better way of doing this, just don't know lol.

Sergey Berezovskiy

I get an error saying 'This row has been removed from a table and does not have any data' What am I missing here?

You have two loops - outer and inner. And you have two problems here. First one is deleting row when match found. On next iteration of inner loop you are trying to get value of deleted row, which throws exception. But even if you'll break inner loop after deleting row, you'll have another problem - modification of rows you are enumerating. So, your code should look like:

foreach (DataRow r1 in dt1.Rows.Cast<DataRow>().ToArray()) // save rows to array
{
    foreach (DataRow r2 in dt2.Rows)
    {
        if (r1.Field<Guid>("C3") == r2.Field<Guid>("C3"))
        {
            r1.Delete();
            break; // break inner loop
        }
    }
}

But I'd go with LINQ approach - get rows for deletion by joining rows on id field, and then delete them from table:

var rowsToDelete = from r1 in dt1.AsEnumerable()
                   join r2 in dt2.AsEnumerable()
                        on r1.Field<Guid>("C3") equals r2.Field<Guid>("C3")
                   select r1;

foreach(DataRow row in rowsToDelete.ToArray())
   row.Delete(); // marks row as deleted;

NOTE: After you use Delete method on existing DataRow, its RowState becomes Deleted. Row remains Deleted until you call AcceptChanges, which removes row from table.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete rows from a data table that exists in another data table

From Dev

Delete rows from table based on condition if other data exists within the table

From Dev

Delete rows in Data table

From Dev

How to SELECT from a table if no data exists in another

From Dev

Delete data from a table by fetching data from another table in mysql

From Dev

Getting Data from a Table, if it exists on another table get it from there

From Dev

Moving table data to another table if table is not exists

From Dev

select rows from table based on data from another table

From Dev

select rows from table based on data from another table

From Dev

Oracle Temporary table columns and data from another table's rows

From Dev

Insert new rows into table but copy data from another row in the table

From Dev

delete multiple rows of a table where this data came from another select in postgres

From Dev

Delete all data rows from an Excel table (apart from the first)

From Dev

Delete Rows of Data From Live Table inside Form?

From Dev

delete rows from a table that are not present in another

From Dev

Delete from table if the id doesn't exists in another table

From Java

Using data.table to select rows by distance from another row

From Dev

My SQL - Refer data from another table rows

From Dev

Insert Data from one table to another leaving the already existing rows

From Dev

MySQL: deleting rows based on a condition with data from another table and NO JOIN

From Dev

My SQL - Refer data from another table rows

From Dev

Mysql - update column from another table data without reducing rows

From Dev

Missing rows when duplicating data from another table using MySQL

From Dev

MySQL delete data from a table

From Dev

Trigger to not delete Data from a table

From Dev

Data wont delete from table

From Dev

MySQL delete data from a table

From Dev

How to remove rows from a data table based on a condition in another data table

From Dev

How to select rows from one data.table to apply in another data.table?

Related Related

  1. 1

    Delete rows from a data table that exists in another data table

  2. 2

    Delete rows from table based on condition if other data exists within the table

  3. 3

    Delete rows in Data table

  4. 4

    How to SELECT from a table if no data exists in another

  5. 5

    Delete data from a table by fetching data from another table in mysql

  6. 6

    Getting Data from a Table, if it exists on another table get it from there

  7. 7

    Moving table data to another table if table is not exists

  8. 8

    select rows from table based on data from another table

  9. 9

    select rows from table based on data from another table

  10. 10

    Oracle Temporary table columns and data from another table's rows

  11. 11

    Insert new rows into table but copy data from another row in the table

  12. 12

    delete multiple rows of a table where this data came from another select in postgres

  13. 13

    Delete all data rows from an Excel table (apart from the first)

  14. 14

    Delete Rows of Data From Live Table inside Form?

  15. 15

    delete rows from a table that are not present in another

  16. 16

    Delete from table if the id doesn't exists in another table

  17. 17

    Using data.table to select rows by distance from another row

  18. 18

    My SQL - Refer data from another table rows

  19. 19

    Insert Data from one table to another leaving the already existing rows

  20. 20

    MySQL: deleting rows based on a condition with data from another table and NO JOIN

  21. 21

    My SQL - Refer data from another table rows

  22. 22

    Mysql - update column from another table data without reducing rows

  23. 23

    Missing rows when duplicating data from another table using MySQL

  24. 24

    MySQL delete data from a table

  25. 25

    Trigger to not delete Data from a table

  26. 26

    Data wont delete from table

  27. 27

    MySQL delete data from a table

  28. 28

    How to remove rows from a data table based on a condition in another data table

  29. 29

    How to select rows from one data.table to apply in another data.table?

HotTag

Archive