The software I'm using, Tekla Structures, returns an IEnumerator of all the filtered object of the database. It returns several hundreds of thousands object. Looping trough the IEnumerator with MoveNext() to add items to an ArrayList takes too long. Is there a way to cast the IEnumerator directly to an ArrayList? Is there a way to use System.Linq.Enumerable.ToArray() for that?
Asked
Active
Viewed 50 times
-4
-
1I guess the accepted answer in this one is what you're looking for: https://stackoverflow.com/questions/1029612/is-there-a-built-in-way-to-convert-ienumerator-to-ienumerable – Arcord Aug 16 '22 at 21:48
-
4No modern (2005+) code would provide "a way to cast the IEnumerator directly to an ArrayList"... You may want to [edit] question to clarify why you expect "ToArray" (or any other conversion method) would be faster than iterating through all items? I would guess that you know of some other restrictions that allow you to make such assumptions - adding those to the post would make the question better. – Alexei Levenkov Aug 16 '22 at 21:59
-
I've run a number of benchmarks looking at the ICollection overload of the `ArrayList` constructor and it is twice as slow as simply iterating an IEnumerable and calling Add. I found that very surprising, but what I'm finding is that a foreach is the fastest you'll get. – David L Aug 16 '22 at 23:38
-
Why do you want an `ArrayList` in the first place? It's ancient and has no reason for existing in modern code. Use either a normal array, or a `List
` – Charlieface Aug 17 '22 at 15:25 -
Because the method I am calling in the software I use require an arraylist. – DavBig Sep 01 '22 at 22:30
1 Answers
0
I don't think there is a faster method than MoveNext().
Even if you write a wrapper class it will try to copy all the elements in the background.

gerdogdu
- 44
- 5