How to avoid an exception...Specific Case Using Linq and a SortedDictionary

Friends,

I have a SortedDictionary and I am using Linq to try and find the int32 key I want to delete, when I know the value.  I know this sounds a bit backward, but...

Anyway, once I discover the int32 key, if it still is in the SortedDictionary, i want to remove that entry.  However, once I do that, I get my exception...
 ..
            If BlendLineETList.ContainsKey(ElapsedTimeToDelete) Then
                      BlendLineETList.Remove(ElapsedTimeToDelete) <---InvalidOperationException!!!!

Once it hits this line, game over!  I get an InvalidOperationException occurred (Collection was modified after the enumerator was instantiated.)

Full code for the entire sub is attached...

What am I doing wrong?  What do I need to do to fix it?  Syntax help requested please!!!

Thanks in advance!

Eric
  ...
Dim BlendLineETList As New SortedDictionary(Of Int32, String)
    

Private Sub DeleteElapsedTimeOfAParticularCar(ByVal CarKey As String)
        SyncLock BlendLineETList
            If BlendLineETList.ContainsValue(CarKey) Then
                Dim KeyResult = From SDValues In BlendLineETList Where SDValues.Value = CarKey _
                Select SDValues
                Dim ETToDelete As Int32 = 0
                For Each SDValue In KeyResult
                    Dim ElapsedTimeToDelete = SDValue.Key
                    If BlendLineETList.ContainsKey(ElapsedTimeToDelete) Then
                        BlendLineETList.Remove(ElapsedTimeToDelete)
                    End If
                Next
            End If
        End SyncLock
    End Sub

Open in new window

indy500fanAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
You cannot modify the value of an enumeration variable--basically I am talking about the XXXX in something like:

    For Each XXXX As Object In SomeList

To modify a collection, you need to use indexing rather than enumerating.
0
 
tovvenkiConnect With a Mentor Commented:
yes kaufmed is correct you need to use index.
I usually loop back from backward and delete from the collection. In this way the index will be properly maintained
for e.g
for( int i=mylist.Count; i!=0; i-- ){
// remove item here.
}

Thanks and regards,
Venki
0
 
indy500fanAuthor Commented:
   All I had to do was this...

Private Sub DeleteElapsedTimeOfAParticularCar(ByVal CarKey As String)
        Dim ElapsedTimeToDelete = 0
        SyncLock BlendLineETList
            If BlendLineETList.ContainsValue(CarKey) Then
                Dim KeyResult = From SDValues In BlendLineETList Where SDValues.Value = CarKey _
                Select SDValues
                For Each SDValue In KeyResult
                    ElapsedTimeToDelete = SDValue.Key
                Next

                If BlendLineETList.ContainsKey(ElapsedTimeToDelete) And ElapsedTimeToDelete > 0 Then
                    BlendLineETList.Remove(ElapsedTimeToDelete)
                End If
            End If
        End SyncLock
    End Sub
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
Assuming KeyResult always contains one record; otherwise, you will only delete the last item returned by the foreach loop (which there is no certain way of guaranteeing what it will be).
0
 
indy500fanAuthor Commented:
I am doing work on the frontend to make sure that only 1 record is returned....

The problem is, I was trying to delete within the for loop.

Once I moved that part of the routine out, it does fine...
0
 
käµfm³d 👽Commented:
Cool.  Glad you got it sorted out then  :)
0
 
indy500fanAuthor Commented:
Their answers led me down the path to come to my own answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.