C# 使用嵌套的foreach循环降低代码的时间复杂度

C# 使用嵌套的foreach循环降低代码的时间复杂度,c#,C#,我是C开发新手,我有一个评论类,如下所示 class Comments { public Id {get; set;} public Text {get;set;} public ParentId {get;set;} public List<Comments> childComments {get;set;} } 我们在主注释中有子注释字段。我将每个注释对象保存为NoSQL DB中的文档。我需要获取这些所有注释,并通过将其所有子注释放置在“chil

我是C开发新手,我有一个评论类,如下所示

class Comments
{
    public Id {get; set;}
    public Text {get;set;}
    public ParentId {get;set;}
    public List<Comments> childComments {get;set;}
}
我们在主注释中有子注释字段。我将每个注释对象保存为NoSQL DB中的文档。我需要获取这些所有注释,并通过将其所有子注释放置在“childComments”字段中,将它们转换为单个注释对象。如果注释位于0级最顶层注释或第一级注释,则ParentId将为null。 我编写了下面的代码来检索它

List<Comments> parentcomments = <from DB>.Where(t => t.ParentId == ObjectId.Empty).ToList();
List<Comments> childcomments = <from DB>.Where(t => t.ParentId != ObjectId.Empty).ToList();

foreach(comment t in parentcomments)
{
    finalCommentTree = AggregateComment(childcomments, t.Id);
}

代码运行良好,没有任何问题,但问题在于时间复杂性。有没有降低时间复杂度和提高性能的方法?

另一种方法:

List<Comments> parentList = new List<Comments>()
{   new Comments() { Id = 1, Text = "Parent1", ParentId = -1 },
    new Comments() { Id = 2, Text = "Parent2", ParentId = -1 },
    new Comments() { Id = 3, Text = "Parent3", ParentId = -1 },
};

List<Comments> childList = new List<Comments>()
{
    new Comments() { Id = 91, Text = "child1", ParentId = 3 },
    new Comments() { Id = 92, Text = "child2", ParentId = 2 },
    new Comments() { Id = 93, Text = "child3", ParentId = 1 },
    new Comments() { Id = 94, Text = "child4", ParentId = 2 },
    new Comments() { Id = 95, Text = "child5", ParentId = 2 },
    new Comments() { Id = 96, Text = "child6", ParentId = 1 },
    new Comments() { Id = 97, Text = "child7", ParentId = 2 }
};
            
List<Comments> k = ( from c in childList
                        join p in parentList
                        on c.ParentId equals p.Id
                        group c by new
                        {
                            c.ParentId
                            ,p.Text
                        } into stdGrp
                        select new Comments
                        {
                            Id = stdGrp.Key.ParentId,
                            Text = stdGrp.Key.Text,
                            ParentId = -1,
                            childComments = stdGrp.OrderBy(j => j.Id).ToList(),
                        }
                        ).ToList();

Comment=注释?@vc74是的,很抱歉造成混淆,我更正了问题。
List<Comments> parentList = new List<Comments>()
{   new Comments() { Id = 1, Text = "Parent1", ParentId = -1 },
    new Comments() { Id = 2, Text = "Parent2", ParentId = -1 },
    new Comments() { Id = 3, Text = "Parent3", ParentId = -1 },
};

List<Comments> childList = new List<Comments>()
{
    new Comments() { Id = 91, Text = "child1", ParentId = 3 },
    new Comments() { Id = 92, Text = "child2", ParentId = 2 },
    new Comments() { Id = 93, Text = "child3", ParentId = 1 },
    new Comments() { Id = 94, Text = "child4", ParentId = 2 },
    new Comments() { Id = 95, Text = "child5", ParentId = 2 },
    new Comments() { Id = 96, Text = "child6", ParentId = 1 },
    new Comments() { Id = 97, Text = "child7", ParentId = 2 }
};
            
List<Comments> k = ( from c in childList
                        join p in parentList
                        on c.ParentId equals p.Id
                        group c by new
                        {
                            c.ParentId
                            ,p.Text
                        } into stdGrp
                        select new Comments
                        {
                            Id = stdGrp.Key.ParentId,
                            Text = stdGrp.Key.Text,
                            ParentId = -1,
                            childComments = stdGrp.OrderBy(j => j.Id).ToList(),
                        }
                        ).ToList();