I am not sure you need to do it iteratively. The common solution is to do it recursively. Most people consider recursion to be easier. I prefer iteration. Please see Iteratively Processing Recursive Data, Such As Directories - TechNet Articles - United States (English) - TechNet Wiki; it describes a relevant algorithm for use of iteration that could be used for treeview data too. And your question is confusing since your subject says iteratively and the body of the question says recursively. Can you describe using words what your requirements are? Do you need to retrieve all items or just the items under a specified id?
Saving Hierarchical (Treeview) object Iteratively with parent and child in C#

I have database table object which is:
[Table("TreeViewDb")]
public class TreeViewDb
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
}
And I have a view model whcih is :
public class TreeView
{
public TreeView()
{
Children = new List<TreeView>();
}
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
// children
public List<TreeView> Children { get; set; }
}
Now I need to save TreeView to the database. During save children or children under children to the nth Level. But my below method only goes to level 3. How can I go to nth Level to save child and parent objects with recursive way?
public bool SaveOrUpdateTreeView(TreeView viewModel)
{
// Level 1
var parentModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = viewModel.ParentId,
Name = viewModel.Name
};
// Save or update object and return primary key
var parentId = _dataRepository.SaveOrUpdateTreeView(parentModel);
// Level 2
foreach (var child in viewModel.Children)
{
var childModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = parentId, // Parent Primary Key
Name = viewModel.Name
};
// Save or update object and return primary key
var childId = _dataRepository.SaveOrUpdateTreeView(childModel);
// Level 3
foreach (var grandChild in child.Children)
{
var grandChildModel = new TreeViewDb
{
Id = viewModel.Id,
ParentId = childId, // Child Primary Key
Name = viewModel.Name
};
_dataRepository.SaveOrUpdateTreeView(grandChildModel);
}
return true;
}
return true;
}
2 answers
Sort by: Most helpful
-
-
Timon Yang-MSFT 9,526 Reputation points
2021-02-25T05:44:19.04+00:00 If you want to store it in the database, what you need should be a list of TreeViewDb.
Try this:
public void AddChildren(List<TreeViewDb> Nodes, TreeView viewModel) { foreach (TreeView thisNode in viewModel.Children) { var childModel = new TreeViewDb { Id = thisNode.Id, ParentId = thisNode.ParentId, Name = thisNode.Name }; Nodes.Add(childModel); AddChildren(Nodes, thisNode); } }
Call it in another method:
List<TreeViewDb> treeNodes = new List<TreeViewDb>(); treeNodes.Add(new TreeViewDb() { Id = rootTreeView.Id,Name = rootTreeView.Name }); AddChildren(treeNodes, rootTreeView);
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.