0

This is my Model for a directory

public class Repositories
{
    public string Id { get; set; }
    public string ParentId { get; set; }
    public string Name { get; set; }
    public string ApplicationName { get; set; }
    public string ApplicationId { get; set; }
    public string Path { get; set; }
    public ICollection<Repositories> RepositoryObjectChildren { get; set; }
    public DateTime CreatedOn { get; set; }
}

This is how I retrieve the directory tree.

public List<Repositories> GetRepositoryTree()
{
    List<Repositories> data = new List<Repositories>();
    try
    {
        data = dbContext.Repositories.Include(x => x.RepositoryObjectChildren).Where(x => x.ParentId == null).ToList();
    }
    catch (Exception e)
    {
        logger.LogError(e, LoggingGlobals.NotFound);
        data = null;
    }
    return data;
}

I am unsure of how to continue from here. How do I save to this Model? Specifically how do I save to the ICollection part? My intention is to create a directory of folders with subfolders that may have subfolders.

halfer
  • 19,824
  • 17
  • 99
  • 186
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • OK, are all 'levels' of the folder structure basically an instance of `Repositories` with or without a `ParentId`? If so, could you not just retrieve them all from the Db as a 'flat' structure then put the sub levels together via a for loop.My logic is that recursion is probably going to be quite slow in linq so retrieve the data and manipulate after. – scgough Apr 03 '19 at 07:57
  • Yeah they are an instance of Repositories. How do I loop them together via a for loop? – JianYA Apr 03 '19 at 08:08
  • See https://stackoverflow.com/questions/2175882/how-to-represent-a-data-tree-in-sql – Ian Mercer Apr 06 '19 at 17:29

1 Answers1

0

As per the comment above, here's a working example of how you could loop over the 'flat' list recursively. I'll keep it basic and you can apply it to your specific use-case.

//my test class for the 'flat' folder structure
public class Item
{
    public string Id { get; set; }
    public string ParentId { get; set; }
    public string title { get; set; }
    public List<Item> subs { get; set; }

    public Item()
    {
        this.Id = "";
        this.ParentId = "";
        this.title = "";
        this.subs = new List<Item>();
    }
}

//example using MVC controller
public class YourController : Controller
{
    //var to hold all the 'flat' data
    private List<Item> fulllist = new List<Item>();

    //recursive method to loop through the flat list
    public List<Item> GetChildren(string parentId)
    {
        List<Item> result = new List<Item>();

        foreach (Item child in fulllist.Where(n=>n.ParentId == parentId))
        {
            child.subs = GetChildren(child.Id);
            result.Add(child);
        }

        return result;
    }

    [HttpGet]
    public ActionResult Index()
    {
        //populate the flat list with test data (multi-level)
        fulllist.Add(new Item()
        {Id = "1", ParentId = "", title = "main 1"});
        fulllist.Add(new Item()
        {Id = "2", ParentId = "", title = "main 2"});
        fulllist.Add(new Item()
        {Id = "3", ParentId = "1", title = "main 1 - sub 1"});
        fulllist.Add(new Item()
        {Id = "4", ParentId = "", title = "main 3"});
        fulllist.Add(new Item()
        {Id = "5", ParentId = "4", title = "main 3 - sub 1"});
        fulllist.Add(new Item()
        {Id = "6", ParentId = "5", title = "main 3 - sub 1 - subsub 1"});
        fulllist.Add(new Item()
        {Id = "7", ParentId = "", title = "main 4"});
        fulllist.Add(new Item()
        {Id = "8", ParentId = "7", title = "main 4 - sub 1"});

        //get from the start of the tree
        var output = GetChildren("");

        return Json(output, JsonRequestBehavior.AllowGet);
    }
}
scgough
  • 5,099
  • 3
  • 30
  • 48
  • Hi! Thank you for your answer. I managed to implement my version of it. However, how do I display it this? Do I use a loop? But there will be an internal loop as well? – JianYA Apr 03 '19 at 15:24