How to find row in datatable and delete all rows before it

Hi,

I have a data table looking something like this:

12345 Tracktor Miami 3456
12345 Car Miami 3456
12345 Van Miami 3456
12345 Tracktor Miami 3456
12345 Car Miami 3456
12345 Van Miami 3456
12345 Tracktor Miami 3456
12345 Car Miami 3456
12345 Van Miami 3456

I need to find the last row containing “Tracktor” in col 2, and delete all rows before it. How can I do that?

1 Like

Hi,

How about the following expression?

dt.AsEnumerable.Skip(dt.Rows.IndexOf(dt.AsEnumerable.Where(Function(r) r(1).ToString="Tracktor").Last)).CopyToDataTable

Regards,

Hi

Hope the below steps would help you resolve this

  1. Let’s take like you have datatable named dt
  2. Now use a assign activity to get the last row with that value in the datatable

lastrow = dt.AsEnumerable().LastOrDefault(Function(row) row.Field(Of String)(1) = “Tracktor”)

Where lastrow is a variable of type System.Data.Datarow

  1. Now use IF condition like this

Lastrow = Nothing

If true it goes to then block where we don’t need to do anything as it is doesn’t have any such value in datatavle
If false it goes to ELSE block where let’s use a assign activity to get the index of that lastrow like this

int_lastrowindex = dt.Rows.IndexOf(lastRow)

where int_lastrowindex is a variable of type int32

  1. Now use a final assign activity like this to remove the rows above to the index we have found

dt = dt.AsEnumerable.Skip(int_lastrowindex).CopyToDataTable

Cheers @Evert_Randers

Hi, thanks for the reply.

I get an error at the If condition activity. It says " Argument ‘Condition’:(2): error BC30452: Operator ‘=’ is not defined for types 'DataRow and 'DataRow#

Working perfect, thanks!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.