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 7073657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:57:21+00:00 2026-05-28T05:57:21+00:00

Consider the following requirement for building a forum App Parent Post – Child Post1

  • 0

Consider the following requirement for building a forum App

Parent Post

- Child Post1

    - Child Post1-1
    - Child Post1-2
        - Child Post1-2-1
- Child Post2
    - Child Post

- Child Post3

Table Structure

tblPost
–

  • PostId
  • ChildPostId
  • Title
  • Post Content
  • UserName

=====================

I can retreive this kind of data using a recursive CTE. I am not sure this is the best approach.

Questions

  • What is the best way to retreive this data using SQL?

  • Is there a better way to load this data using an ORM?

  • If we go the SQL route, what is the best way to Load this data into a class like shown below:

    public class Post {
      public int PostId {get;set;}
      public string PostTitle {get;set;}
      public string PostContent {get;set;}
      public string PostedBy {get;set;}
      public IEnumerable<Post> ChildPosts {get;set;}
    }
    
  • How about displaying this kind of data say using the razor syntax for a view??

  • 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-05-28T05:57:21+00:00Added an answer on May 28, 2026 at 5:57 am

    According to your comment you are open to suggestions about improving your current database schema in which you basically have a post_id and a child_post_id columns to perform the hierarchical relationship.

    So let’s proceed:

    What is the best way to retreive this data using SQL?

    I would recommend you taking a look at the following article which illustrates a very nice technique for managing such hierarchical data in a very efficient way. It uses the The Nested Set Model in which you define sets with left and right nodes and then you are able to build the entire tree with a single SQL query:

    enter image description here

    Is there a better way to load this data using an ORM?

    There are ways doing this using an ORM such as NHibernate and EF but I will leave this for next time. You might consider splitting your questions into multiple SO questions as the subject is quite broad. If you learn how to do this using plain ADO.NET you will gather far better understanding of the underlying techniques that are involved so that tomorrow you decide to use such an ORM you will already know what to look for in order of efficient queries.

    How about displaying this kind of data say using the razor syntax for
    a view??

    Once you have constructed your hierarchical model it’s extremely simple. All you have to do is to define a custom display template for the Post type in which you would invoke the display template for all child posts.

    So assuming the following model:

    public class Post
    {
        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public IEnumerable<Post> ChildPosts { get; set; }
    }
    

    and the following controller (in which I obviously have hardcoded the values but after reading the tutorial I have linked to in the beginning of my post you will be able to construct this model with a single SQL query):

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // Hardcoding the model here, but you could use the 
            // Nested Set Model technique I have linked to 
            // in order to build this model from your database
            var post = new Post
            {
                PostId = 1,
                PostTitle = "Parent Post",
                ChildPosts = new[]
                {
                    new Post 
                    {
                        PostId = 2,
                        PostTitle = "Child Post 1",
                        ChildPosts = new[]
                        {
                            new Post 
                            {
                                PostId = 3,
                                PostTitle = "Child Post 1-1",
                                ChildPosts = new[]
                                {
                                    new Post
                                    {
                                        PostId = 4,
                                        PostTitle = "Child Post 1-2-1"
                                    }
                                }
                            },
                            new Post 
                            {
                                PostId = 5,
                                PostTitle = "Child Post 1-2"
                            },
                        }
                    },
    
                    new Post 
                    {
                        PostId = 6,
                        PostTitle = "Child Post 2",
                        ChildPosts = new[]
                        {
                            new Post
                            {
                                PostId = 7,
                                PostTitle = "Child Post"
                            }
                        }
                    },
                    new Post 
                    {
                        PostId = 8,
                        PostTitle = "Child Post 3"
                    },
                }
            };
            return View(post);
        }
    }
    

    and then you would have an ~/Views/Home/Index.cshtml view:

    @model Post
    <ul>
        @Html.DisplayForModel()
    </ul>
    

    and of course a corresponding display template (~/Views/Home/DisplayTemplates/Post.cshtml) which will be recursive in our case to render the full tree:

    @model Post
    <li>
        @Html.DisplayFor(x => x.PostTitle)
        <ul>
            @Html.DisplayFor(x => x.ChildPosts)
        </ul>
    </li>
    

    and of course the final result is what one might expect:

    enter image description here


    UPDATE:

    As requested in the comments section here’s one example of how one might populate the Post model. Let’s assume that you have followed the nested set model to design your database table:

    CREATE TABLE posts (id int primary key, left int, right int, title nvarchar(100));
    

    and that you have filled it with the posts:

    INSERT INTO posts (id, left, right, title) VALUES (1, 1, 16, 'Parent Post');
    INSERT INTO posts (id, left, right, title) VALUES (2, 2, 9, 'Child Post1');
    INSERT INTO posts (id, left, right, title) VALUES (3, 3, 4, 'Child Post1-1');
    INSERT INTO posts (id, left, right, title) VALUES (4, 5, 8, 'Child Post1-2');
    INSERT INTO posts (id, left, right, title) VALUES (5, 6, 7, 'Child Post1-2-1');
    INSERT INTO posts (id, left, right, title) VALUES (6, 10, 13, 'Child Post2');
    INSERT INTO posts (id, left, right, title) VALUES (7, 11, 12, 'Child Post');
    INSERT INTO posts (id, left, right, title) VALUES (8, 14, 15, 'Child Post3');
    

    Now you could fetch them.

    But as always before actually doing something you describe what you want to do. That is: you define a contract:

    public interface IPostsRepository
    {
        Post GetPost();
    }
    

    Now you get to the doing. In this case we will use plain ADO.NET to query the database and built the Post object. We will use an iterative algorithm with a stack to build the tree but you could also use a recursive algorithm:

    public class PostsRepositoryAdoNet: IPostsRepository
    {
        private readonly string _connectionString;
        public PostsRepositoryAdoNet(string connectionString)
        {
            _connectionString = connectionString;
        }
    
        private class Scalar
        {
            public int Depth { get; set; }
            public Post Post { get; set; }
        }
    
        public Post GetPost()
        {
            using (var conn = new SqlConnection(_connectionString))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText =
                @"
                    SELECT p.id, p.title, (COUNT(parent.title) - 1) AS depth
                    FROM posts AS p, posts AS parent
                    WHERE p.left BETWEEN parent.left AND parent.right
                    GROUP BY p.title
                    ORDER BY p.left;
                ";
                using (var reader = cmd.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        return null;
                    }
    
                    var nodes = new Stack<Post>();
                    var scalar = FromDataReader(reader);
                    var rootNode = scalar.Post;
                    int currentDepth = 0;
                    var currentNode = rootNode;
                    while (reader.Read())
                    {
                        var depth = reader.GetInt32(reader.GetOrdinal("depth"));
                        if (depth > currentDepth)
                        {
                            nodes.Push(currentNode);
                            currentDepth = depth;
                        }
                        else if (depth < currentDepth)
                        {
                            while (depth < currentDepth)
                            {
                                --currentDepth;
                                nodes.Pop();
                            }
                        }
                        scalar = FromDataReader(reader);
                        currentNode = scalar.Post;
                        var p = nodes.Peek();
                        if (p.ChildPosts == null)
                        {
                            p.ChildPosts = new List<Post>();
                        }
                        p.ChildPosts.Add(currentNode);
                    }
                    nodes.Clear();
                    return rootNode;
                }
            }
        }
    
        private Scalar FromDataReader(DbDataReader reader)
        {
            return new Scalar
            {
                Depth = reader.GetInt32(reader.GetOrdinal("depth")),
                Post = new Post
                {
                    PostId = reader.GetInt32(reader.GetOrdinal("id")),
                    PostTitle = reader.GetString(reader.GetOrdinal("title"))
                }
            };
        }
    }
    

    Now that we have this repository we could bring the pieces together:

    public class HomeController : Controller
    {
        private readonly IPostsRepository _repository;
        public HomeController(IPostsRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            var post = _repository.GetPost();
            return View(post);
        }
    }
    

    and the last part is to configure your favorite Dependency Injection framework to inject the desired implementation of the repository and since we have only one so far that would be PostsRepositoryAdoNet. And if tomorrow you decide to switch to an ORM all you have to do is to write the corresponding repository implementing the IPostsRepository interface.

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

Sidebar

Related Questions

Consider the following Parent/Child relationship. What I would like to know is how I
Consider the following code (from a requirement that says that 3 is special for
I need an efficient algorithm in Python for the following requirement. Consider I am
Consider the following implementation of Web app and Client - that uses Spring/Hibernate for
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //

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.