【问题标题】:Recursion for Directory Paths目录路径的递归
【发布时间】:2019-04-03 06:47:06
【问题描述】:

这是我的目录模型

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; }
}

这就是我检索目录树的方式。

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;
}

我不确定如何从这里继续。如何保存到这个模型?具体来说,如何保存到 ICollection 部分?我的意图是创建一个包含子文件夹的文件夹目录,这些子文件夹可能有子文件夹。

【问题讨论】:

  • 好的,文件夹结构的所有“级别”基本上都是Repositories 的实例,有或没有ParentId?如果是这样,您能否不只是从 Db 中将它们全部检索为“平面”结构,然后通过 for 循环将子级别放在一起。我的逻辑是 linq 中的递归可能会很慢,因此检索数据并进行操作之后。
  • 是的,它们是存储库的一个实例。如何通过 for 循环将它们循环在一起?

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

根据上面的评论,这是一个工作示例,说明如何递归地遍历“平面”列表。我将保持基本,您可以将其应用于您的特定用例。

//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);
    }
}

【讨论】:

  • 嗨!谢谢您的回答。我设法实现了我的版本。但是,我该如何显示它呢?我使用循环吗?但也会有内部循环?
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2021-11-20
  • 2018-10-19
相关资源
最近更新 更多