Compare two recursive objects and delete items in C#

Viewed 34

I have two class with recursive item. The class are given below:

public class TreeView
    {
        public TreeView()
        {
            Children = new List<TreeView>();
        }

        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }

        // children
        public List<TreeView> Children { get; set; }
    }

    public class TreeViewModel
    {
        public TreeViewModel()
        {
            Children = new List<TreeViewModel>();
        }

        public TreeView TreeView { get; set; }

        // children
        public List<TreeViewModel> Children { get; set; }
    }

Now I have two objects which are:

var treeView = new TreeView();
var model = new TreeViewModel();

Now I would like to compare treeView object with model object with all children and delete which objects exists in treeView but not in model. I have prepared the below method for that.

public static void DeleteItem()
        {
            var treeView = new TreeView();
            var model = new TreeViewModel();

            // level 2
            foreach (var child in treeView.Children)
            {
                var modelChild = model.Children.Where(x => x.TreeView.Id == child.Id).FirstOrDefault();

                if (modelChild != null)
                {
                    // level 3
                    foreach (var grandChild in child.Children)
                    {
                        var modelGrandChild = modelChild.Children.Where(x => x.TreeView.Id == grandChild.Id).FirstOrDefault();

                        if (modelGrandChild == null)
                        {
                            // delete grandChild
                        }
                    }
                }
                else
                {
                    foreach (var item in child.Children)
                    {
                        // delete item
                    }

                    // finally delete child object
                }
            }
        }

Is there any better approach to compare two objects and separate the child objects to delete.

1 Answers

We need to recurse through the TreeView, checking each child at each level whether the equivalent model exists.

We recurse by passing each TreeView child node with it's matching model. If there is no parent model then we do not check at all, we just take null. Then if we have no equivalent child (or there is not even a parent) we delete.

public static void DeleteMissingChildren(TreeView treeView, TreeViewModel model)
{
    foreach (var child in treeView.Children)
    {
        TreeViewModel modelChild =
            model == null
            ? null
            : model.Children.FirstOrDefault(x => x.TreeView.Id == child.Id);
        DeleteMissingChildren(child, modelChild);
        if(modelChild == null)
            delete(child);  //whatever
    }
}
Related