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.