0
votes

I have a datatable with fields like so:

ID     startdate    enddate      price
2      03/26/2018   03/27/2018   100
56     03/26/2018   03/27/2018   85
88     03/27/2018   03/28/2018   12
14     03/27/2018   03/28/2018   66

and an array of datarows like so:

[2     03/26/2018  03/27/2018    100,
 25    03/27/2018  03/28/2018    66]

How can I remove out of the datatable the rows that lie on the same dates as the datarows but do no have the same IDs as the datarow array?

1

1 Answers

0
votes

so what I had done in order to accomplish this task is to first create a list of the ids from the array of datarow:

var targetIds = targetDataRows.Select(r => r.Field<int>("ID")).ToList();

then looped through the targetDataRows to get their specific dates and rows from the datatable that do not match the targetIds and remove them from the overall datatable

foreach(var row in targetDataRows)
{
     var startDate = (DateTime)row["startdate"];
     var endDate = (DateTime)row["enddate"];
     var nonMatchingRows = (from row in overallDataTable.AsEnumerable()
                            where !targetIds.Contains(row.Field<int>("ID")) && DateTime.Compare(row.Field<DateTime>("startdate"), startDate) == 0 && DateTime.Compare(row.Field<DateTime>("enddate"), endDate) == 0
                            select row).ToList();
     foreach(var nonMatchingRow in nonMatchingRows)
     {
           overallDataTable.Tables[0].Rows.Remove(nonMatchingRow);
     }
}