none
collection was modified enumeration operation may not execute. RRS feed

  • Question

  • Hi ,

           I got Error in when i perform operation on list (collection was modified enumeration operation may not execute.)

    {

    Hashtable objexportinfo = ExportController.userinfo;

    List<ExportInfo> list = (List<ExportInfo>)objexportinfo[CurrentUser.UserName];

    string Percentage = string.Empty;

    string finalReturn = string.Empty;

    //if (ExportController.GetCount != null)

     

    foreach (ExportInfo lstInfo in list)

    {

    if (lstInfo.count != null)

    //if (list[0].count != null)

    {

    //string[] countAndThreadId = ExportController.GetCount.Split(',');

    //string[] countAndThreadId = list[0].count.Split(',');

    string[] countAndThreadId = lstInfo.count.Split(',');

    if (countAndThreadId.Length > 0)

    {

    double s = Convert.ToInt32(countAndThreadId[0]);

    double percentage = Convert.ToDouble((((double)s / (double)Total) * 100));

    Percentage = percentage.ToString();

    finalReturn = Percentage +

    "," + countAndThreadId[1].ToString();

    if (Percentage == "100")

    {

    Deletelist(Threadid);

    }

    }

    }

    }

    //Deletelist(Threadid);

    return finalReturn;

    public string getCount(string Threadid)

    public void Deletelist(string Threadid)

    {

    Hashtable objexportinfo = ExportController.userinfo;

    List<ExportInfo> list = (List<ExportInfo>)objexportinfo[CurrentUser.UserName];

    list.RemoveAll(s => s.ThreadId ==

    Convert.ToInt32(Threadid));

    }

    }


    Ashish

    Monday, November 19, 2012 12:39 PM

Answers

  • Hi,

    if you want to remove items from a collection, use a counting for loop and start at the highest index, decrement by one

    for(int i = mylist.Count - 1; i >= 0; i--)
    {
       //remove here
    }

    Regards,

      Thorsten

    Monday, November 19, 2012 12:58 PM