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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:48:11+00:00 2026-05-16T16:48:11+00:00

I’m trying to understand how to build a self referencing model for hierachical data.

  • 0

I’m trying to understand how to build a self referencing model for hierachical data. Eventually i will be creating a category tree.

I don’t anything in tables yet. After I have the structure nailed down then I will create the tables. My existing Object is defined like this:

public class Categories
{
    public Int64 catID { get; set; }
    public Int64? parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }
}

My fake repository populates Categories like this:

// Fake hardcoded list of categories
private static IQueryable<Categories> fakeCategories = new List<Categories> {
    new Categories { catID = 1, parentCatID = null, catName = "Root", catDescription = "" },
    new Categories { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
    new Categories { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
    new Categories { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
    new Categories { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
    new Categories { catID = 6, parentCatID = null, catName = "Another Root", catDescription = "" },
    new Categories { catID = 7, parentCatID = null, catName = "Ze German Root", catDescription = "" },
    new Categories { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
    new Categories { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();

I am stuck on how to make this a self referencing object which I can send to a view for display. I was thinking that the controller would be something like this:

public ActionResult CategoryTree()
{
    var cats = fakeRepository.Categories.ToList();
    return View(cats);
}

I don’t know if I’m approaching this correctly. I would display the category tree using a custom HtmlHelper method that recurses.

How would I get Categories to be a self referencing object?

Edit: rephrased question

I’m starting to think that I’m asking questions that are over my head 🙂

Please examine this code:

[Table]
public class Category
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public Int64 catID { get; set; }
    public Int64? parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }

    internal EntityRef<Category> _category;
    [Association(ThisKey = "parentCatID", Storage = "_category")]
    public Category category {
        get { return _category.Entity; }
        internal set { _category.Entity = value; }
    }
}

This code compiles and I don’t have to change my fake repository. I feel like im missing something though. I’m not sure how to test this to see if it works. I’m pushing against the limits of my knowledge.

I’m not even 100% sure of what the correct question is. I’m going to go play with this… see if I get errors or if it works the way i expect. I’ll probably edit here again.

Edit 2

It appears that Category.category is just returning null. And, it doesn’t seem to be in a list of any type. I’m not sure if I set up the relationship correctly.

Any thoughts?

  • 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-16T16:48:11+00:00Added an answer on May 16, 2026 at 4:48 pm
    public class Category   // an instance of the class is just ONE category
    {
        public Int64 Id { get; set; }
        public Category Parent { get; set; }        // A parent link, EF will  handle this for you using an Association
        public List<Category> Children {get; set;}  // Replace this when you move to EF or L2S
        public string Name { get; set; }
        public string Description { get; set; }
    }
    

    If you use the Linq2Sql or Entity Framework designer in Visual Studio instead you can create an Entity (or Class) called ‘Category’ and then an association back to itself. Either designer will automatically create a collection property on the entity/class that allows you to navigate to the collection of children.

    Here’s what your Linq2Sql diagram might look like:

    alt text

    And here’s what the association should look like:
    alt text

    Your fake repository can then simply use the Linq2Sql classes like so:-

    Category root = new Category() { Name = "Root", Parent = null };
    Category child1 = new Category() { Name = "Child 1", Parent = root };
    Category child2 = new Category() { Name = "Child 2", Parent = root };
    

    And Linq2Sql will ‘magically’ set the ‘Children’ collection on ‘Root’ for you.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.