简体   繁体   中英

Reduce Time Complexity of Code with nested foreach loops

I am new to C# development, I have a "Comments" Class as below.

class Comments
{
    public Id {get; set;}
    public Text {get;set;}
    public ParentId {get;set;}
    public List<Comments> childComments {get;set;}
}

We have child comments field within main comments. I am saving each comment object as a Document in NoSQL DB. I need to fetch these all comments and convert them into single comment object by placing all of its child comments inside 'childComments' field. ParentId will be null if comment is at level 0(top most level or first level comment). I wrote the below code to retrieve it.

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


public List<Comments> AggregateComment(List<Comments> childcomments, ObjectId parentId)
{
       List<Comments> recursiveObjects = new List<Comments>();
       foreach (Comments item in childcomments.Where(x => x.ParentId.Equals(t.ParentId)))
       {
            recursiveObjects.Add(new Comments
            {
                    Id = item.Id,
                    Text = item.Text,
                    childComments = AggregateComment(childcomments, item.Id)
            });
       }
       return recursiveObjects;
}

Code works good without any issues, but problem is with time complexity. Is there a way to reduce time complexity and improve performance?

Another approach:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM