简体   繁体   中英

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

    private void findManagerForSelectedDate(String dateSelected)
    {
        dateSelected = dateTimePicker1.Value.ToShortDateString();

        List<String> managerNames = new List<String>();
        foreach(var item in managers)
        {
            foreach (var subitem in item)
            {
                CalendarModel c = subitem;
                Console.WriteLine(c.date);
                c.name = new CultureInfo("en-US", false).TextInfo.ToTitleCase(c.name);
                if (userSelection.Count > 0)
                {
                    foreach (var addedUser in userSelection)
                    // Crashing here with An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
                    {
                        if (!addedUser.Contains(c.name))
                        {
                            userSelection.Add(c.name);
                        }
                    }
                } else
                {
                    userSelection.Add(c.name);
                }

It was able to iterate thru my list 3 times then on the 4th time, it crashed.

The Exception details says {"Collection was modified; enumeration operation may not execute."}

You can add your users into a temp list and add this temp list to the original list at the end :

    private void findManagerForSelectedDate(String dateSelected)
    {
        dateSelected = dateTimePicker1.Value.ToShortDateString();

        List<string> tempUsersToAdd = new List<string>();
        List<String> managerNames = new List<String>();
        foreach(var item in managers)
        {
            foreach (var subitem in item)
            {
                CalendarModel c = subitem;
                Console.WriteLine(c.date);
                c.name = new CultureInfo("en-US", false).TextInfo.ToTitleCase(c.name);
                if (userSelection.Count > 0)
                {
                    foreach (var addedUser in userSelection)
                    {
                        if (!addedUser.Contains(c.name))
                        {
                            tempUsersToAdd.Add(c.name);
                        }
                    }
                }
                else
                {
                    tempUsersToAdd.Add(c.name);
                }
            }
        }

        userSelection.AddRange(tempUsersToAdd);

You can't edit a collection while you iterate through it with a foreach loop

a hack would be to iterate through a copy of your collection:

foreach (var addedUser in userSelection.ToArray())

and keep the same code

you can use linq to test if c.name is in userSelection

if(!userSelection.Exists( u => u.Contains(c.name))) userSelection.Add(c.name);

of course you need using System.Linq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM