【发布时间】: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