For each Row - Logic Help Needed

Hi Team,

I know this is silly but i just want to use this kind of logic in one of the modules.

So I have a table in a datatable DTTest like shown below:-

Name Age
Robot 1
Robot 23
UiPath 4
Temp 5
Yes 67

And I wanted to loop through the dt such that if Nth row and (N-1) row data for Name matches then perform task ABC. Whereas If is not not matching, perform XYZ Task.

I was easily able to perform this logic.


for each row in DTTest:
if dtTest.Rows(index).Item(“Name”).ToString = dtTest.Rows(index-1).Item(“Name”).ToString
(perform ABC)
else (perform XYZ)

My problem: While this logic is working smoothly, I am getting index out of range error when we talk about the first row case ie (index=0) is being compared with (index - ‘-1’), which obviously goes out of range with respect to DT range.

Like if it is 1st row (index=0), then dont care about previous row

Please help me create a logic such that it handles the 1st row case too in my logic.

Thanks and Regards,

Go for a for each (not a for each row)
var: item, Values: Enumerable.Range(1, YourDataTable.Rows.Count-1) | TypeArgument: int32

item represent the row index of nth Col starting with 1
item1 represent the row index of n-1th Col

within the for each you can now check in an if Activity:
If: YourDataTableVar.Rows(item)(ColName).toString.Equals(YourDataTableVar.Rows(item-1)(ColName).toString
Then: doABC
Else: doXYZ

Put another If around the existing If and have it be “index > 0”

Then the If you already have won’t process unless the index is 1 or higher.