Find first row in datatable that contains a specific string, and delete all rows after this

Hi, I have a DT with a few thousand rows. In col B there are multiple word text strings. Some of these strings are randomly repeated multiple times. I need to find the first occurence of a specific string, (i.e. “This Is A Test String”, and when found delete ALL rows after. So if “This Is A Test String” is found at row 18 of 100 rows, everything from nr 19 - 100 should be deleted.

I found ways to do this but it involves too many steps, using Excel and so on. Does anyone know a simple way of doing this?

Get index of row, where you have the first occurence of this text:

int rowIndex = DT.Rows.IndexOf(DT.AsEnumerable.FirstOrDefault(function(row) row("ColumnName").toString.Equals("This Is A Test String")))

where DT is your DataTable variable and ColumnName is the column contains this specific string.

Use this index in the second expression to get datatable without rows after this index:

DT.AsEnumerable.Take(rowIndex + 1).CopyToDataTable
1 Like

@Botter
First clone the datatable and use for each row in datatable.Inside of it use while condition inside the condition add Not currentrow(“colB”).tostring.equal("This Is A Test String” ) then use add datarow row activity to add current row to cloned datatable.when the condition fails,it comes out of loop.