How to get the Rowindex within Lambda function

Hi, I am trying to get the rowindex of a datable with the following method:

assigns: i = From p In dt.Rows.IndexOf(Where p.Item(0).ToString.Equals(“…”))

Why I need the index ? To overwrite the row with write range.

@B.D
give atry on following:

Assign
left side: arrIdx (integer array)
right side:

(From i in Enumerable.Range(0, yourDataTableVar.Rows.Count)
Where yourDataTableVar.Rows(i)(0).toString.Trim.Equals(“…”)
Select i).toArray

you will retrieve all matching index or an empty array if there were no matches found

1 Like

Hi ppr, thanks.
Can you elaborate your code ? I don’t understand this part:

why are you trimming it ?

also why do I need to set a range. And why Enumerable ?

@B.D
derived from your inputs a statement was formulated to retrieve an int array with indexes of the rows within a datatable matching a particular value for a particular column

The main idea of statement is:

  • Create a sequence with all row indexes
  • filter out the the indexes where the column test is not valid
  • return the result as an integer array

we do a trim when we check with equals to be more defensive when data comes e.g. from excel and risks that undetected blanks are trailing, leading (feel free to remove it)

1 Like

Other way like below

Enumerable.Range(0,YourDataTable.Rows.Count).
Where(Function(i) YourDataTable.Rows(i)(0).ToString().Equals(“…”)).
ToArray

2 Likes

Thank you park. Always good to have alternative solutions.

Thanks for the explanation

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