344
Views
9
Comments
Solved
How to get a specified consecutive number of elements of a list?
Question

I have list containing several elements (lets say 10). I want to get the first 5 elements of the list in a new list (Copy). Later on I could need the same but starting at element 3 up until element 8. 

In C# I would use the GetRange method but I find nothing like this in OutSystems. Is this possible without a foreach loop and if so, how?

2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

Hi Vincent,

Why not just use the startindex and maxiterations on the foreach ?

for optics : A ForEach + ListAppend takes about the same space as a ListFilter + ListAppendAll

for performance : It is faster than a ListFilter, as that will allways go through the whole list

for semantics : I find it easier to understand what's going on (but that's subjective, i know)

Dorine

PS you might get a warning if startindex is not 0 and count is not matching linecount of a widget, but that's all it is, a warning, ignore it.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Dorine,

You are right, if the List isn't too big this is probably just as fast as the ListFilter trick on Traditional Web (or just as fast, I'm not sure what the overhead is of the ListAppend).

2021-09-06 15-09-53
Dorine Boudry
 
MVP

I would imagine that internally, a ListFilter with a condition is implemented as a loop going over every item checking if the condition is true, then appending to the ListFilter result list.  

Or is there a trick about using the ListFilter to avoid that ?

Otherwise, I think a Listfilter is as fast as using startindex and maxcount if we take all items, and is always slower if we only take part of the items.

(not that the difference in performance is likely to be important unless the amount of items is very large)

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I think ListFilter uses specific C# constructions that are faster than the equivalent in OutSystems.

2021-09-06 15-09-53
Dorine Boudry
 
MVP


this post was begging for a little test :



2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Whaddayaknow, ListFilter seems slower than For Each + ListAppend, who would've thought! Thanks for the test!

2021-04-17 09-53-48
José Gonçalves

Hi Vincent,

if ListFilter had a "position" property could be done, but without it I couldn't find a way.

Why don't you create an extension for the GetRange method in C#?

Regards

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Vincent,

With Traditional Web, you can use the CurrentRowNumber property of the List in combination with a ListFilter to get a certain subsection of a List in a new List. Unfortunately, this doesn't work for Reactive Web, you'd have to resort to JavaScript.

2021-11-19 11-12-44
Rui Mendes

Hello Vicent,

I took some time and set up an example for you with OutSystems technique.

I hope it's something like this you're looking for.


greetings,
Rui

ps: for some reason the site won't let me publish the example oml here, so I had to publish it in forge
https://www.outsystems.com/forge/Component_Overview.aspx?ProjectId=11868

rangeExempl.png
2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

Hi Vincent,

Why not just use the startindex and maxiterations on the foreach ?

for optics : A ForEach + ListAppend takes about the same space as a ListFilter + ListAppendAll

for performance : It is faster than a ListFilter, as that will allways go through the whole list

for semantics : I find it easier to understand what's going on (but that's subjective, i know)

Dorine

PS you might get a warning if startindex is not 0 and count is not matching linecount of a widget, but that's all it is, a warning, ignore it.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Dorine,

You are right, if the List isn't too big this is probably just as fast as the ListFilter trick on Traditional Web (or just as fast, I'm not sure what the overhead is of the ListAppend).

2021-09-06 15-09-53
Dorine Boudry
 
MVP

I would imagine that internally, a ListFilter with a condition is implemented as a loop going over every item checking if the condition is true, then appending to the ListFilter result list.  

Or is there a trick about using the ListFilter to avoid that ?

Otherwise, I think a Listfilter is as fast as using startindex and maxcount if we take all items, and is always slower if we only take part of the items.

(not that the difference in performance is likely to be important unless the amount of items is very large)

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I think ListFilter uses specific C# constructions that are faster than the equivalent in OutSystems.

2021-09-06 15-09-53
Dorine Boudry
 
MVP


this post was begging for a little test :



2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Whaddayaknow, ListFilter seems slower than For Each + ListAppend, who would've thought! Thanks for the test!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.