For Each 100 Rows

Hi Guys,

I have an Excel File which has a dynamic amount of rows.

I am taking info from each row and adding to an API Body, which limits the call to 100.

I have read the Excel and what I’d like to do is use a Do While and add the first 100 rows from the DT (with which I am using a For Each to manipulate the strings for the API Body).

I then would Ideally delete these used rows, and repeat with the next 100, if there are more (there also may be less than 100 rows).

I then make the HTTP Request for each 100 rows which I have dealt with, and then
The Exit condition for the Do While would then be if the original DT.Rows.Count = 0.

I am having difficulty in moving on to the next 100 rows, if the indeed exist, I am over-complicating it and would like to achieve this in minimal code.

Any help would be great!

@Kyleb91

In the interest of keeping the code quality high while also maintaining the head on your hair, I would recommend you use Queues.
Upon starting, convert each row into a queue item and then use them as you need. That way, you won’t need to remove the rows from datatable or face concurrent modification issues (such as - removing rows from a table whilst still iterating it).

I hope this helps.
Cheers.

1 Like

Just include a condition within your for each row in Master DT when a counter = 100 or rowcount of DT, then do your API call, reset counter.

3 Likes

@Kyleb91
Have a Look Here in how a datable can be Segmented for a Processing

1 Like

You can use logic as simple as this, dont bother in deleting rows already read, as it only will slow you down…

2 Likes

Thanks for options guys! Much appreciated! @Matt_S your solution is the most simplistic approach so went with that (after some additional conditions for cases of less than 100).

@ppr this method is fantastic, I will need to study this.

Thanks again @bcorrea and @RPAForEveryone also!

1 Like

@Kyleb91
for getting only the top 100 rows following line used in a for each row can maybe help as well

YourDataTableVar.AsEnumarable.Take(100).CopyToDataTable

1 Like

Thanks @ppr I had initially used this too. Let’s say there are 150 rows, how would you approach the next loop taking care of just the remaining 50 rows that have not been dealt with, and If statement and Counter?

this is handling with the skip take as the take is not error throwing when less rows are remaining
find xaml here:
ForEach_Segment_RowSets.xaml (11.8 KB)

1 Like

@Kyleb91 - The .Take() method won’t throw an error, it just grabs the remainder if there are less items than the indicated amount

1 Like

Thanks @Dave, yeah I noted that, but once looping back to the DT, it would subsequently take the same initial 100.

1 Like

Oh sorry, I misunderstood. It looks like ppr mentioned the .skip(counter).take(amount) way, which is the way I always have handled things as well since I like the simplicity and ease of reading. Then after the loop you just update the counter to be counter = counter + amount This way on the first iteration counter = 0, then second time it is 100, etc

I for sure respect all answers, but is there really anything more simple than mine? Or was that not understood?

2 Likes

I think your answer is best & generally the simplest, but in a lot of workflows I try to avoid using “math tricks” for readability because a lot of uipath developers are new to programming or don’t have much background in it. It is harder to tell immediately what it’s doing (modulo isn’t a common concept for non-programmers).

Commenting or annotating should fix that issue of course, but I just go with the things that make more sense and are easier to maintain for the most people

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