ERROR : Collection was modified; enumeration operation may not execute – While converting Entity Collection To List
Views (779)
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


Like
Report
*This post is locked for comments