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