title: Best way to iterate through Hashtable and conditionally remove entries in VB.NET
body: <p>In VB.NET, I have a HashTable that I would like to iterate through and conditionally remove entries from. I've written the following code that does the job perfectly, but I'd like to know if there are any creative ways to simplify the code. It just doesn't seem right to have to create a second list to perform this operation.</p>
  
  <p>Here's what I've written:</p>
  
  <pre><code>Dim ModsToRemove As New List(Of String)
  For Each ModKey As DictionaryEntry In ModHashTable
      If ModKey.Key.ToString.Contains("Criteria") Then
          ModsToRemove.Add(ModKey.Key.ToString)
      End If
  Next
  For Each ModKey As String In ModsToRemove
      ModHashTable.Remove(ModKey)
  Next
  </code></pre>
  
  <p>Is there another way to perform the same operation that doesn't require the creation of a second list and a second loop? Is it possible to remove entries from something you are iterating through without throwing an error in VB.NET? Is doing so universally a bad idea?</p>