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

  • Home
  • SEARCH
  • 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 6224577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:41:47+00:00 2026-05-24T08:41:47+00:00

I am developing an MVC app that retrieves data from a table in SQL

  • 0

I am developing an MVC app that retrieves data from a table in SQL Server that is structured like so:

+-----------------------------------+
| Id | Name   | Hierarchy   | Depth |
|-----------------------------------|
| 01 | Justin | /           |     0 |
| 02 | Chris  | /1          |     1 |
| 03 | Beth   | /1/1        |     2 |
+-----------------------------------+

The example data in the Hierarchy column is the string representation of the hierarchyid datatype, and the Depth column is computed using the hierarchyid::GetLevel() method.

Using Entity Framework 4.1, I have mapped the above table to this class:

public class Node {
    public int Id { get; set; }
    public string Name { get; set; }
    public string HierarchyPath { get; set; } // String representation of the hierarchyid
    public int Depth { get; set; }
}

I want to use this information to display a graphical representation of the hierarchy to the user using the JS Visualizations Toolkit, which requires the data to be structured:

var node = {
    id: 1,
    name: 'Justin'
    children: [{
        id: 2,
        name: 'Chris',
        children: [{
            id: 3,
            name: 'Beth',
            children: []
        }]
    }]
}

I’m having trouble developing the logic to convert a list of my models into a structured JSON object. Any suggestions?

  • 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-24T08:41:47+00:00Added an answer on May 24, 2026 at 8:41 am

    EDIT: I don’t have time to fix the answer below right now, but given the extra information in the question, I suspect you want to keep a Dictionary<int, HierarchicalNode> rather than a List<HierarchicalNode> so that you’re not relying on any ordering…


    I would forget about the JSON representation to start with, and concentrate on building an in-memory POCO representation of the hierarchy. To do that, I’d use something like this:

    class HierarchicalNode
    {
        private readonly List<HierarchicalNode> children =
            new List<HierarchicalNode>();        
        public List<HierarchicalNode> Children { get { return children; } }
    
        private readonly string name;
        public string Name { get { return name; } }
    
        private readonly int id;
        public int Id { get { return id; } }
    
        public HierarchicalNode(string name, int id)
        {
            this.name = name;
            this.id = id;
        }
    }
    

    Then build up the tree like this:

    // Make sure we get everything in a sensible order, parents before children
    var query = context.Nodes.OrderBy(x => x.Depth);
    
    var root = new HierarchicalNode("Root", 0);
    foreach (var node in query)
    {       
        var current = root;
        foreach (string part = node.HierarchyPath.Split(new[] {'/'},
                                       StringSplitOptions.RemoveEmptyEntries))
        {
            int parsedPart = int.Parse(part);
            current = current.Children[parsedPart - 1];
        }
        current.Children.Add(new HierarchicalNode(node.Name, node.Id));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing a Sencha Touch MVC app that pulls data from json store (thats set
I am developing asp.net mvc web app with MongoDB as the data storage. I
I am developing an ASP.NET MVC app and I've been looking into using Data
I'm developing a web app with MVC 2.0. I'm storing my data in Windows
We are developing an Iphone app that will talk to the ASP.net MVC 3.0
I'm developing my first ASP.NET MVC app and what I'd like to accomplish is
I have a VS 2008, .NET 3.5 targeted MVC.NET app. I am developing on
I'm developing a MVC .Net site and would like to implement a CMS system.
I'm developing an MVC framework in PHP from scratch; mostly for the learning experience
I am developing an ASP.NET MVC app with custom membership and role providers. My

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.