I am trying to use recursion to go up a hierarchy, and i get this error:
Collection was modified; enumeration operation may not execute.
My assumption here is that when it enters the function each time, it is using the same parentRolesCopy and not a different one, so when it enters in the second time it changes parentRolesCopy to be different in the original function call.
How can I get around this?
private IEnumerable<string> GetAllParentRoles(string role)
{
// GET EACH PARENT ROLE
var parentroles = //code that gets any parents of the role passed in
//RECURSIVELY CALL THIS FUNCTION TO KEEP GETTING PARENTS OF PARENT ROLES UNTIL NONE LEFT
var parentRolesCopy = parentroles;
foreach (var parentrole in parentRolesCopy)
{
parentroles.AddRange(GetAllParentRoles(parentrole));
}
return parentroles;
}