Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:51:08+00:00 2026-06-10T09:51:08+00:00

I have a list of nested comments that essentially form a tree. //Non-Relevant properties

  • 0

I have a list of nested comments that essentially form a tree.

//Non-Relevant properties (Like Body, Creator etc) excluded
internal class Comment
{
    public int ID { get; set; }
    public int ItemID { get; set; }
    public int ParentItemID { get; set; }
    public List<Comment> ChildComments { get; set; }
}

First, I get a list of Posts from a database, then I get all Comments where ItemID == Posts[all].id

I want to loop through a flat List and create a nested, or tree List.

It goes like this:

If the ParentItemID == 0, then we have a top level comment. If ParentItemID > 0 we need to match ParentItemID to a Comment ID, and add the child Comment to the Parent Comments List.

Where I’m stuck is, I’ve only used recursion in the past to navigate files and folders. This doesn’t allow me to instantiate a collection and hold on to it through subsequent recursions.

It also seems silly to instantiate the list outside the recursion, and then loop through all items each time I want to add an item.

I’m sure there is a good solid pattern for doing this, I just can’t find it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:51:10+00:00Added an answer on June 10, 2026 at 9:51 am

    You can just loop through all comments and get the child comments for each:

    foreach (Comment comment in comments) {
      comment.ChildComments =
        comments.Where(c => c.ParentItemID == comment.ItemID).ToList();
    }
    

    Another alternative that would perform better (if that is needed) would be to group the comments on the ParentItemID and create a Dictionary<int, List<Comment>> from that, then just loop through the comments like above and get the lists from the dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a nested list something like this: PLACES = ( ('CA', 'Canada', (
I have Python nested list that I'm trying to organize and eventually count number
I have a nested list, List<List<String>> intable; where I would like to sort all
I have a nested list of ul elements. I would like to decrease the
I have a large nested list that I am trying to animate using jquery
Little help need with jquery sortable. I have a nested list like so: <div
I have a unordered list nested inside of an unordered list like below: <ul
I want to turn an ordinary nested list, into a collapsable tree like structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.