SAP loop a table and check next or previous values if it is same

I have an excel file that lists sales order numbers, line numbers, and qty.
There are sometimes same sales order numbers.
The robot first read the excel file, and then inputs the data to SAP.

SO Line qty
30xxxxxx 10 100
30xxxxxx 20 200
30aaaaaa 10 100
30bbbbbb 10 10

I need to combine the same sales order and process together.
When the robot loops through the table, it clicks “save” at the end of each iteration, and then goes to the next line.
But I want to avoid to click “save” if the next SO number is the same as the current SO number .
If the next SO number is the same as current one, proceed with the next line input.
If the next SO number is different as the current one, save the current data, and go back to the first step.

I tried to check if the current SO number equals with the next (or previous) one (image below)but it does not work because the first row and the last does not have the previous or next row.

How do I loop through this kind of process?


I think it’s better to use Dictionary which is grouped by SO

dict = dt.AsEnumerable.GroupBy(Function(r) r("SO").ToString).ToDictionary(Function(g) g.Key,Function(g) g.CopyToDataTable)

dict is Dictionary<string,DataTable> type.


Then we can iterate SO in outer ForEach and iterate rows which has same SO in inner ForEachRow.