I think you should not use foreach, because you are going to change the list (delete the entry). It will work fine for the first iteration code, but it will throw an exception in the second iteration, saying that "the collection has been changed." Instead of foreach, you can use a simple loop.
source
share