An IAsyncCursor can only be enumerated once
Answers
Jak
908
Points
132
Posts
|
Try explicitly enumerating the results by calling ToList(). Change
to
Posted On:
19-Dec-2017 05:12
Not working. getting same error - Smith 20-Dec-2017 05:07
|
Priya
1194
Points
33
Posts
|
Add .ToList() before for-each loop:
Full code:
Posted On:
20-Dec-2017 05:18
That worked! Can you explain why it works? - Smith 20-Dec-2017 05:28
it works because using ToList you are retrieving ALL results from the db in this list, from now on linq methods aren't going to work on the database. - Priya 20-Dec-2017 05:43
The main cause .ToList() works is that when you use an IEnumerable, the collection is iterated in a lazy way. In this case, each time the foreach loop gets an item, the query retrieves the item from your db or whatever you are using, if you want to iterate again it wont be able to redo the query. When you use .ToList(), all the items in the IEnumerable are processed and saved as a list. Then you can use the created list as many times as you want. - Brian 20-Dec-2017 05:45
|
Blog
Active User (0)
No Active User!