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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:39:25+00:00 2026-06-10T23:39:25+00:00

this code give me and Exception Collection was modified; enumeration operation may not execute

  • 0

this code give me and Exception

Collection was modified; enumeration operation may not execute

i use Entity Framework to work with data

            foreach (OfflineMessage omc in _offMsgs)
            {

                var OimDB = new OimDBEntities();

                if (omc.MsgTo == e.MessageData.ToHeader.Uri)
                {
                    var offlineMessage = new OfflineMessage
                    {
                        Delivered = false,
                        MsgContent = omc.MsgContent,
                        MsgFrom = omc.MsgFrom,
                        MsgTime = omc.MsgTime,
                        MsgTo = omc.MsgTo,
                        ID = OimDB.OfflineMessages.NextId(f => f.ID)

                    };

                    oimRepository.InsertOIM(offlineMessage);

                    //InsertData(omc.MsgFrom, omc.MsgTo, omc.MsgContent, omc.MsgTime);
                }
            }
            _toHeader = e.MessageData.ToHeader.Uri;
        }
  • 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-10T23:39:27+00:00Added an answer on June 10, 2026 at 11:39 pm
    foreach (OfflineMessage omc in _offMsgs)
                    {
                        if (omc.MsgTo == _toHeader)
                        {
                            _offMsgs.Remove(omc);
                        }
                    }
    

    You are removing elements from the collection while iterating over it. You can’t do this. Instead of removing, add them to a temporary list, and after the foreach loop, Remove your items.

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

Sidebar

Related Questions

Telephone Manager give me Null phone number while i have use this code -
I am using entity framework code first and I want to mark a collection
I was testing Thrift on java, but this code give me some errors.. ClientVoiceToTextHandler
Look at this code: class MyClass(): # Why does this give me NameError: name
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
I'm wondering why does Visual Studio code analysis give a warning with this little
The following code when compiled give this error I cannot understand why this is
I know how to do this... I'll give example code below. But I can't
Looking at this code: try: ... # do something except: raise Exception('XYZ has gone
This code below allows me to find the word error in all my files

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.