我的任务是根据一些数据结构生成目录文件。数据如下所示:
class ToCItem
{
public Dictionary<int, string> path;
public int page;
}
对于这样的示例数据:
ToCItem
{
path = { 1 => "chapter 1" },
page = 1;
}
ToCItem
{
path = { 1 => "chapter 1", 2 => "section 1" },
page = 2;
}
ToCItem
{
path = { 1 => "chapter 1", 2 => "section 2" },
page = 6;
}
ToCItem
{
path = { 1 => "chapter 1", 2 => "section 2", 3 => "image" },
page = 7;
}
ToCItem
{
path = { 1 => "summary" },
page = 8;
}
我需要这样的输出:
.chapter 1: 1
..section 1: 2
..section 2: 6
...image: 7
.summary: 8
(点是标签)
我想不出任何算法来做到这一点。我的第一个想法是按每个层次结构级别对项目进行分组,然后执行以下操作:
foreach (var group in paths.GroupBy(p => p.Path[1]))
{
if (group.Key != null)
{
Console.Write("\t");
Console.WriteLine(group.Key);
}
var grouped2 = group.GroupBy(g => g.Path.ContainsKey(2) ? g.Path[2] : null);
foreach (var group2 in grouped2)
{
if (group2.Key != null)
{
{
Console.Write("\t\t");
Console.WriteLine(group2.Key);
}
}
var grouped3 = group.GroupBy(g => g.Path.ContainsKey(3) ? g.Path[3] : null);
foreach (var group3 in grouped3)
{
if (group3.Key != null)
{
Console.Write("\t\t\t");
Console.WriteLine(group3.Key);
}
}
}
}
但后来我只得到层次结构而不是实际路径。此外,这不会随着更深层次的层次而扩展。有没有人有任何想法?