Attempting to remove any element from a collection within a foreach loop will result in the above mentioned error.
To stop the error simply add a .ToList() or .ToArray() within the foreach loop as seen below.
List distinctList = new List();
foreach (string item in distinctList.ToList())
By calling .ToList(), it copies the value of the collection to a separate collection and nothing else has access to it. It does not even have a variable name. As such nothing can modify it in the loop.