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 8460035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:29:43+00:00 2026-06-10T13:29:43+00:00

So the scenario in a nutshell: I have an entity (EF4) that has a

  • 0

So the scenario in a nutshell: I have an entity (EF4) that has a foreign key to the primary key in the same entity (hierarchical structure) e.g.

MyEntityId (Primary Key)
ParentMyEntityId (Foreign key to Primary Key)

If I have a MyEntityList List<MyEntity> where the EntityState for both is Unchanged:

entity 1 - {MyEntityId = 10, ParentMyEntityId = null}
entity 2 - {MyEntityId = 11, ParentMyEntityId = 10}

and then I do this:

//Initially has 2 items in 'in' clause - iterates once and then exits because the EntityState of the second item has changed to Modified
foreach(MyEntity m in MyEntityList.Where(e => e.EntityState == System.Data.EntityState.Unchanged))
{
     db.DeleteObject(m);
}

The first MyEntity is deleted, but the second changes to “Modified” and the foreach doesn’t run a second time – I’m guessing due to the foreign key constraint.

However if I do:

//Iterates twice, even though the EntityState of the second item has changed to Modified
foreach(MyEntity m in MyEntityList.Where(e => e.EntityState == System.Data.EntityState.Unchanged).ToList())
{
     db.DeleteObject(m);
}

Both entities are deleted (which is the desired effect).

Whilst I have a solution, I’m interested in why this happens, I was always under the impression that the iterator “set” that was defined at the start of the foreach loop remained the same, or threw a runtime error if you tried to modify it.

Should I not be using Where? Is there a better way to do this?

  • 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-06-10T13:29:45+00:00Added an answer on June 10, 2026 at 1:29 pm

    The iterator set is defined before the loop runs because you execute ToList (if you didn’t do this it wouldn’t be well defined).

    So the iteration source is constant. But not the object you iterate over. The object references you get are constant but not the objects pointed to by them.

    The version without ToList is equivalent to:

    foreach(MyEntity m in MyEntityList) //always 2 items
    {
         //loop body always called two times
         if (e.EntityState == System.Data.EntityState.Unchanged) //2 times
            db.DeleteObject(m); //1 time
    }
    

    The ToList-version is equivalent to:

    var copy = MyEntityList.Where(e => e.EntityState == System.Data.EntityState.Unchanged).ToList(); //always 2 items
    foreach(MyEntity m in copy)
    {
         //loop body always called two times
         db.DeleteObject(m); //2 times
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have a TFS Build (TFS2008) that has several targets override: BuildNumberOverrideTarget, AfterGet,
Scenario: Most smartphones have a high resolution camera that generates photos that may range
Scenario: I have a Question which has 2 radio buttons & two subforms: subformA
Scenario: I have several services that I want to be discovered by different clients.
Scenario: UserControl that has a read-only TextBox and a Button. TextBox.Text is modified and
Scenario I have a TreeView that is bound to ObservableCollection<T> . The collection gets
Scenario- I have a method that returns an object retrieved from an NSMutableArray similar
Scenario: You have an ASP.Net webpage that should display the next image in a
SCENARIO (iOS 5) I currently have a UITableView. It has 30 table rows The
Scenario A image website that has basic features such as upload, comment, like, follow

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.