Mail message delete list item in run time error For Each: Collection was modified; enumeration operation may not execute

Hello,

I’m trying to find a workaround for this error, I’m using Foreach mailmessages and inside i need to remove an email from the list and continue looping through the rest of the mailmessages.

Has anyone run into this , what can i do to solve this?

1 Like

@Ayasoliman Most probably you cannot perform the operation in that way, since you’re trying to remove an item from the Collection which you are already using it to loop. There are other ways to remove items based on a condition such as using Linq or you can use a long approach of copying items to another List.

If you want to use Linq, can you provide the condition based on which you would want to remove the mail items ?

1 Like

you can use continue activity in then part of If activity
In If activity ,put your that condition , for that you want to delete mail message

So instead delete message us continue

let me know if any issue or problem

If any urgent then ping me on my mail id sg2070156@gmail.com

Hey @Ayasoliman,

Just add suffix .ToList to your collection in foreach. PFB Ref

This should do the trick :slight_smile:

Thanks

2 Likes

this worked! thanks!

1 Like

I opted to create a new list instead of modifying but now i tested @Nithinkrishna solution and it modified the list without error, thanks

2 Likes

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