English 中文(简体)
C#: 对收集进行了修改;统计工作不得执行[复制]
原标题:C#: Collection was modified; enumeration operation may not execute [duplicate]

我的目标是删除我申请中的用户名单。 但是,我们不能从这一错误的底线上去。 大约一名pl子拯救我。

if (txtEmailID.Text.Length > 0)
{
    users = UserRespository.GetUserName(txtEmailID.Text);
    bool isUserAvailable=false;
    foreach (EduvisionUser aUser in users) // Exception thrown in this line
    {
        isUserAvailable = true;
        if(!aUser.Activated)
        {
            users.Remove(aUser);
        }
    }
    if (users.Count == 0 && isUserAvailable)
    {
        DeactivatedUserMessage();
        return;
    }
}
最佳回答

你试图从名单上删除一个用户,你正在ugh笑。

这是不可能的。 最好制定一份新名单,并增加其中的良好名单,而不是删除坏名单。

if (txtEmailID.Text.Length > 0)
    {
        //@new list
        List<EduvisionUser> listOfAcceptedUsers = new List<EduvisionUser>()**

        users = UserRespository.GetUserName(txtEmailID.Text);
        bool isUserAvailable=false;
        foreach (EduvisionUser aUser in users) --->***Exception thrown in this line***
        {
            isUserAvailable = true;

            //Add user to list instead of deleting
            if(aUser.Activated)
            {
                ListOfAcceptedUsers.Add(aUser);
            }
        }

        //check new list instead of old one
        if (ListOfAcceptedUsers.Count == 0 && isUserAvailable)
        {
            DeactivatedUserMessage();
            return;
        }

    }
问题回答

在您重新编辑时,你可以修改收集,同时附上<条码>。 典型选择:

  • Use a for loop instead
  • Create a separate collection of the items you want to act on, then iterate over that.

第二种做法的实例:

List<EduvisionUser> usersToRemove = new List<EduvisionUser>();
foreach (EduvisionUser aUser in users) --->***Exception thrown in this line***
{
    isUserAvailable = true;
    if(!aUser.Activated)
    {
        usersToRemove.Add(aUser);
    }
}
foreach (EduvisionUser userToRemove in usersToRemove)
{
    users.Remove(userToRemove);
}

另一种选择是,如果你重新使用<代码>List<T>?

isUserAvailable = users.Count > 0;
users.RemoveAll(user => !user.Activated);

你可以这样做。 供餐

for( int i =0; i< users.Count; i++ ) --->***Exception thrown in this line***
 {
  EduvisionUser aUser = users[i];
  isUserAvailable = true;
  if(!aUser.Activated)
  {
    users.Remove(aUser);
    i--;
  }
 }

在列举时,你不能修改收集工作。 不是仅仅删除你需要的东西,而是让垃圾收集者照顾其他东西:

users = users.Where(x => x.Activated);

更糟的是,只从存放处选择你需要的东西:

users = UserRespository.GetUserName(txtEmailID.Text).Where(x => x.Activated);

我的目标是从工作中心删除一份工作证明书,但在选择“工作能力”等例外情况时,“收集经修改;统计工作不得” 任何想法? 感谢

删除方法:

try {

            if (!this.DataWorkspace.ApplicationData.WorkCalendars.CanDelete)
            {
                this.ShowMessageBox("", "", MessageBoxOption.Ok);
                return;
            }

            if (this.WorkCalendars.SelectedItem != null)
            {
                if ((this.WorkCalendars.SelectedItem.FindCalendarWPs.Count() > 0) || (this.WorkCalendars.SelectedItem.FindCalendarWPs1.Count() > 0))
                {
                    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke
        (() =>
        {
            RadWindow.Alert(" ");
        });
                    return;
                }
                var y = DataWorkspace.ApplicationData.WorkCalendarDays.Where(w => w.WorkCalendar.Id == WorkCalendars.SelectedItem.Id).Execute().AsEnumerable();

                foreach (var item in y)
                {
                    if(item.WorkingHoursCollection != null && item.WorkingHoursCollection.Count() > 0)
                        foreach (var WH in item.WorkingHoursCollection)
                        {
                            WH.Delete();
                        }
                    item.Delete();

                }
                if (this.WorkCalendars.SelectedItem == this.DataWorkspace.ApplicationData.WorkCalendars.Where(U => U.Id == this.WorkCalendars.SelectedItem.Id).SingleOrDefault())
                {
                    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke
       (() =>
       {
           RadWindow.Alert(" ");
       });
                    return;
                }

                this.WorkCalendars.SelectedItem.Delete();
                this.Save();
            }

        }
        catch (Exception ex)
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke
          (() =>
          {
              var msg = new LightSwitchApplication.Presentation.GeneralViews.ExceptionMessage();
              msg.DataContext = ex;
              msg.ShowDialog();
          });
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签