Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:24:14+00:00 2026-05-28T20:24:14+00:00

I have a webpage with a form that is used to edit some object.

  • 0

I have a webpage with a form that is used to edit some object. This object contains a Collection of other objects defined like this:

Public Overridable Property Employees As List(Of Employee)

On a form I can delete an employee, add a new one or modify existing one. When I click save new values are sent to the server. On a server I check if the user exists. If exists then I modify its values, if it does not exist then I add it. All employees that exist on the server and were not sent are marked as deleted (State changed to EntityState.Deleted). I try to use the following code (dbCollection = database entities, newCollection = collection sent from the form):

    For Each item In dbCollection
        Dim dbItem = item
        Dim newTask = newCollection.FirstOrDefault(Function(i) i.Id = dbItem.Id)


        If newTask Is Nothing Then
            Me.Entry(item).State = EntityState.Deleted
        Else
            Me.Entry(item).CurrentValues.SetValues(newTask)
            newCollection.Remove(newTask)
        End If
    Next

    For Each item In newCollection
        dbCollection.Add(item)
    Next

    Me.SaveChanges() 

This code does not work, because changing to EntityState.Deleted removes the object from collection, and for each loop breaks, since the collection is modified…

I know that I can overcome this problem by using a for loop or adding objects to delete to some other list first, but I hope maybe there is a pattern that would make my code nicer.

Thanks in advance for all suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:24:15+00:00Added an answer on May 28, 2026 at 8:24 pm

    Replace…

    For Each item In dbCollection
    

    …by:

    For Each item In dbCollection.ToList()
    

    ToList() will create a copy of the collection (only the references, not the objects themselves). dbCollection.ToList() is another collection than dbCollection so that you safely can modify the dbCollection without getting the “collection has been modified” exception in the For Each loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header on my webpage that contains the some javascript for some
I have webpage with a form like this: <html> <head> ... </head> <body> <form
I currently have a webpage where an iframe contains data that is stored into
I have a webpage with a form element and a popup window (opened by
I'm using django, and have a static webpage with a GET form, and about
I have a few asp:textbox controls in a form on a webpage, below is
I have a webpage that pulls information from a database, converts it to .csv
I have a webpage (say www.example.com/a.asp) .. I am having an iframe in this
I have a webpage that is taking way too long and need to optimize
I have a webpage on my site that displays a table, reloads the XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.