从数据库生成目录文件夹结构 [英] Generating a directory folder structure from database
问题描述
在循环中发现自己.我有一个数据库表,该表定义了一个目录文件夹结构,其中子文件夹的数量可能是无限的.
Finding myself in loopie loops. I have a database table that defines a directory folder structure with potentially infinite number of subfolders.
最终结果文件夹结构应如下所示,但逻辑应允许更改此结构要求:
The end result folder structure should look like this, but the logic should allow this structure requirement to change:
为上述文件夹结构提供此数据:
Given this data for the above folder structure:
最重要的字段是 id
和 pid
. NULL
中的 pid
代表顶级文件夹(电子邮件,TM应用程序,TM争议).所有其他文件夹都是子文件夹,它们分为3级,存储在 level_count
字段中.不确定我是否确实需要 level_count
字段.我一直试图使逻辑灵活".尽可能. pid
定义文件夹的直接父级:
The most important fields are id
and pid
. pid
of NULL
represents the top level folders (Email, TM Applications, TM Disputes). All other folders are subfolders which go down 3 levels stored in level_count
field. Not sure I really need level_count
field though. I've been trying to make the logic as "flexible" as possible. pid
defines a folder's immediate parent:
我当前的解决方案还不够好,因为它不能处理无限" 个级别,它仅支持三个级别.我不想不必知道级别数.
My current solution is not good enough because it doesn't handle the "infinite" number of levels, it only supports three levels. I prefer not to have to know the number of levels.
如果可能的话,我希望能够保留核心逻辑,并且我不想更改这种方式,即先创建所有父文件夹,然后再返回以创建子文件夹.相反,我想进入最深的层次,创建这些文件夹,然后备份到父母.如果我觉得合理,我认为这段代码代表了这个想法.
I want to be able to keep the core logic if possible and I do not want to change this in a way that creates all parent folders first, and then goes back to create subfolders. Instead I want to go down the deepest level, create those folders, then back up to parents. I think this code represents that idea, if I'm making sense.
foreach (DataRow r in dtParentFolders.Rows) // these are the 3 parent rows with null pid
{
int parentFolderId = Convert.ToInt32(r["id"]);
string parentFolderName = r["folder_name"].ToString();
//Create folder
Console.WriteLine(parentFolderName);
DataTable dt = GetFolders(parentFolderId);
foreach (DataRow r2 in dt.Rows)
{
parentFolderId = Convert.ToInt32(r2["id"]);
CreateFolder(r2);
dt = GetFolders(parentFolderId);
foreach (DataRow r3 in dt.Rows)
{
parentFolderId = Convert.ToInt32(r3["id"]);
CreateFolder(r3);
dt = GetFolders(parentFolderId);
}
}
}
推荐答案
我希望这可以在某种程度上为您提供帮助.
I hope this can help you in some way.
public class Record
{
public int Id { get; set; }
public int PId { get; set; }
public string Name { get; set; }
}
public static void Main()
{
var records = new List<Record>()
{
new Record { Id = 1, Name = "MainDir1", PId = 0 },
new Record { Id = 2, Name = "MainDir2", PId = 0 },
new Record { Id = 3, Name = "MainDir3", PId = 0 },
new Record { Id = 4, Name = "SubDir1", PId = 1 },
new Record { Id = 5, Name = "SubDir2", PId = 2 },
new Record { Id = 6, Name = "SubSubDir1", PId = 5 },
new Record { Id = 7, Name = "SubSubDir2", PId = 5 },
new Record { Id = 8, Name = "SubSubDir3", PId = 5 },
new Record { Id = 9, Name = "SubSubDir4", PId = 5 },
new Record { Id = 10, Name = "SubSubDir5", PId = 5 },
};
var node = new Directory(0, null, null);
records
.OrderBy(x => x.PId)
.ThenBy(x => x.Id)
.ThenBy(x => x.Name)
.ToList()
.ForEach(x => node.AddChild(x.Name, x.Id, x.PId));
node.Print();
}
public class Directory
{
public Directory(int id, string name, Directory parent)
{
this.Id = id;
this.Name = name;
this.Parent = parent;
this.Indentation = parent is null ? 0 : parent.Indentation + 1;
this.Children = new HashSet<Directory>();
}
public int Id { get; set; }
public int Indentation { get; set; }
public string Name { get; set; }
public Directory Parent { get; set; }
public ICollection<Directory> Children { get; set; }
public void AddChild (string name, int id, int parentId)
{
if (this.Id == parentId)
{
this.Children.Add(new Directory(id, name, this));
return;
}
foreach (var child in this.Children)
{
child.AddChild(name, id, parentId);
}
}
public void Print()
{
Console.WriteLine($"{new string(' ', this.Indentation * 4)}{this.Name}");
foreach (var child in this.Children)
{
child.Print();
}
}
}
这篇关于从数据库生成目录文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!