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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:56:23+00:00 2026-05-29T05:56:23+00:00

The code represent small scale of my problem: public class Category { public Guid

  • 0

The code represent small scale of my problem:

public class Category
{
    public Guid CategoryID { get; set; }
    public string Name { get; set; }
    public Guid? ParentID { get; set; }
    public bool IsTop { get; set; }
    public string Description { get; set; }

    public virtual Category parentCategory { get; set; }
}

When I use this class in Entity Framework, it generates only one relation of parent and child categories.

How can I tell to semantically separate the properties, and generate two different relations in SQL Server one for getting all child categories with (child of child relationship(recursive top-down)), and the other for getting all parent categories (parent of parent(recursive bottom-up))? Something like this:

public virtual ICollection<Category> childCategories { get; set;} 
public virtual ICollection<Category> parentCategories { get; set;}

I tried it with modelBuilder but from there I can only get one level of detail.

  • 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-29T05:56:25+00:00Added an answer on May 29, 2026 at 5:56 am

    I’ve had the problem of retrieving all child nodes to a depth of n come up in one of my projects as the classic supervisor/employee self-referencing relationship on an Employee table in my model. As Slauma and Milracle pointed out, EF won’t help you to retrieve all nodes to a depth of n under a specified parent. However, I was able to solve this problem using a Breadth-first search algorithm in my repository. Please note, that my goal was not only to retrieve all the child nodes, but to do so quickly, as using recursive LINQ queries was taking more than two minutes for the top levels of management. Using this method, it now executes in less than two seconds.

    public IEnumerable<string> GetAllSubordinateEmployeeIdsByUserId(string userId)
    {
        // Retrieve only the fields that create the self-referencing relationship from all nodes
        var employees = (from e in GetAllEmployees()
                         select new { e.Id, e.SupervisorId });
        // Dictionary with optimal size for searching
        Dictionary<string, string> dicEmployees = new Dictionary<string, string>(employees.Count() * 4);
        // This queue holds any subordinate employees we find so that we may eventually identify their subordinates as well
        Queue<string> subordinates = new Queue<string>();
        // This list holds the child nodes we're searching for
        List<string> subordinateIds = new List<string>();
    
        // Load the dictionary with all nodes
        foreach (var e in employees)
        {
            dicEmployees.Add(e.Id, e.SupervisorId);
        }
    
        // Get the key (employee's ID) for each value (employee's supervisor's ID) that matches the value we passed in
        var directReports = (from d in dicEmployees
                             where d.Value == userId
                             select d.Key);
    
        // Add the child nodes to the queue
        foreach (var d in directReports)
        {
            subordinates.Enqueue(d);
        }
    
        // While the queue has a node in it...
        while (subordinates.Count > 0)
        {
            // Retrieve the children of the next node in the queue
            var node = subordinates.Dequeue();
            var childNodes = (from e in dicEmployees
                              where e.Value == node
                              select e.Key);
            if (childNodes.Count() != 0)
            {
                // Add the child nodes to the queue
                foreach (var c in childNodes)
                {
                    subordinates.Enqueue(c);
                }
            }
            // Add the node from the queue to the list of child nodes
            subordinateIds.Add(node);
        }
    
        return subordinateIds.AsEnumerable();
    }
    

    Also, as a footnote, I was able to increase the efficiency of look-ups in the dictionary with help from this Dictionary optimization article.

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

Sidebar

Related Questions

This code represents in small scale my problem: public class Person { public int
I have a data structure that represents C# code like this: class Namespace: string
The problem can be found in the following code: def debug[T](format: String, arg1:T, arg2:Any,
If I have a set of small string values, and I want to fetch
What does this bit of code represent? I know it's some kind of if
So I am writing a Java code to represent a heap sort and to
I have a code base that makes extensive use of files to represent a
Given the Java code below, what's the closest you could represent these two static
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
I'm developing a OCR tool reeds a set of symbols which represent 2 bits

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.