I have a tree construct in my application which contains hierarchical data of users.
public class User {
//a lot of properties here like Id, Name, Last Name, etc.
public IEnumerable<User> Employees {get;set;}
}
Now I need to replace certain record in this tree with new item.
var changedUsers = GetChangedUsers();
// Replace all changed user fields in original hierarchy
// (only FirstName, LastName, etc. without touching Employees field)
Is there an elegant way to achieve this?
EDIT:
I have tried to loop with recursion, but stuck with updating record.
private void ReplaceUserInHierarchy(User modifiedUser, List<User> users)
{
foreach (var user in users)
{
if (user.Id == modifiedUser.Id)
{
//we should update here somehow
return;
}
ReplaceUserInHierarchy(modifiedUser, user.Employees);
}
}
changedUsers