Please wait, loading...

 

ERROR : Collection was modified; enumeration operation may not execute – While converting Entity Collection To List

July 9, 2020

I recently Show 1 of my old note with this error which was pending for post, so thought of sharing it,

While converting EntityRefrence to List- Entity and then processing this List-Entity in For-each Loop got below error

Collection was modified; enumeration operation may not execute

Back then at 1st Glance it was not clear what was the error but debugging it pointed the issue,

  • Code with the Error:  Error is with ForEach Loop in dictionary object 
EntityCollection nonMandatoryProcessRule = FetchAllProcessRules(organizationService, serviceTypeGUID, categoryTypeGUID, departmentTypeGUID, CustomerTypeBOOL, false , 0);
          
            List<Entity> nonMandatoryProcessRuleLIST = new List<Entity>(nonMandatoryProcessRule.Entities);
          
            foreach (Entity processRuleRecordET in nonMandatoryProcessRuleLIST)
            {
                if (!ValidateProcessRuleConditions.ValidateProcessRuleCondition(processRuleRecordET, currentEntityET , organizationService))
                {
                    try
                    {
                        nonMandatoryProcessRuleLIST.Remove(processRuleRecordET);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error in removing " + processRuleRecordET+"  check  " + ex.Message);
                    }
                }
            }
  • Code after Resolving the Error

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651
Microsoft Dynamics Community Profile

Learn more