Index was outside the bounds of the array 1.03

Hi,

does anyone know how to solve this problem:
on my dataDt (which is a range B:B of some excel file, so just 1 column of texts)

im running linq:
dataDt.Rows.Cast(of System.Data.DataRow)().Tolist().Indexof(dataDt.AsEnumerable().Where(Function(row) row(0).Tostring().Contains(“searchedtext”)).ToArray()(0))+1

My hope was i would get the number of row in which “searchedtext” has first appearance.

Result is i get error:
Index was outside the bounds of the array

Would greately appreciate any ideas to fix my code

Hi @Zdzichu_Map

the error occurs when the where condition returns no rows, so toarray()(0) is accessed on an empty array; fix it by using firstordefault and checking for nothing, or use this safer linq:

dataDt.asenumerable().
select(function(r, i) new with {.idx = i, .val = r(0).tostring}).
where(function(x) x.val.contains(“searchedtext”)).
select(function(x) x.idx + 1).
firstordefault()

this returns the row number of the first match or 0 if not found.

If helpful, mark as solution. Happy automation with UiPath

Thank you,

your code works perfectly.

have a great day
kind regards
Zdzichu

1 Like

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