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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tovvenkiCommented:
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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.