My compliment for your presentation of your question!
This is how I would do it - or at least my
first second iteration
*
. Note there's no "dtNew" - I just remove the unwanted rows from "dtOld". If you want to keep "dtOld" around you would have to copy the rows.
int inclusionSpan = 2;
bool[] keepRow = new bool[dtOld.Rows.Count + inclusionSpan * 2];
int colorColumnIndex = dtOld.Columns.IndexOf("Color");
for (int rowIndex = 0; rowIndex < dtOld.Rows.Count; rowIndex++)
{
if (dtOld.Rows[rowIndex].Field<string>(colorColumnIndex) != "White")
{
for (int includeOffset = 0; includeOffset <= inclusionSpan * 2; includeOffset++)
keepRow[rowIndex + includeOffset] = true;
rowIndex += inclusionSpan;
}
}
for (int rowIndex = dtOld.Rows.Count - 1; rowIndex >= 0 ; rowIndex--)
{
if (! keepRow[rowIndex + inclusionSpan])
dtOld.Rows.RemoveAt(rowIndex);
}
*
: Tired, maybe there's a flaw somewhere with the inclusionSpan, would have to test it.