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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:25:30+00:00 2026-05-29T19:25:30+00:00

I am using a nested set model and want to be able to select

  • 0

I am using a nested set model and want to be able to select 10 parent items, as well as 10 child items for each parent, which are ordered.

I am trying to write a nested comment system, whereby a reply to a comment will be signified by having left and right values within its ‘parent’ comment.

I am having a real headache reducing the load times of large lists of items being retrieved and believe if I could do the above in a single Linq statement then I might be able to save a lot more time than making repeated calls to the db to get 10 children for each parent.

I use a this statement to get the parent(or root items) from the datacontext.

var query = context.Items
                   .Where(x => !context.Items.Any(y => y.LeftPos < x.LeftPos
                                                    && y.RightPos > x.RightPos))
                   .OrderBy(x => x.Id)
                   .Take(pageSize)
                   .ToList();

The above code is getting the outermost (parent) items in the nested set model by checking there are not any other items with left and right values outside of theirs.

So, rather than looping through the parentItems with a foreach (which I am doing currently) and making 10 or (pageSize) calls to the db on each iteration, how can I take 10 children of each parent node with a Linq statement?

EDIT:

I am using the nested set model. My items have left and right positions (leftPos and rightPos). So a child is an object with left and right values within the left and right values of another object, which in turn would be the parent.

1 a 4
  2 b 3

so if I have a lot of items

1 a 4
 2 b 3
5 c 10
 6 d 7
 8 e 9
11 f 14
 12 g 13

….

Is there a way I can select x amount of children from each parent using Linq?

Any help appreciated

  • 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-29T19:25:36+00:00Added an answer on May 29, 2026 at 7:25 pm
        class Program
        {
            static void Main(string[] args)
            {
                List<A> lst = new List<A>();
    
                for (int j = 1; j < 4; j++)
                {
                    var tmp = new A() { Value = j * 1000 };
                    for (int i = 0; i < 150; i++)
                    {
                        tmp.SubItems.Add(new B { Value = i + 1, Parent = tmp });
                    }
                    lst.Add(tmp);
                }
    
                List<B> result = lst.SelectMany(x => x.SubItems.Take(10)).ToList();
            }
        }
    
        public class A
        {
            public A()
            {
                SubItems = new List<B>();
            }
    
            public int Value { get; set; }
            public List<B> SubItems { get; set; }
        }
    
    
        public class B
        {
            public int Value { get; set; }
            public A Parent { get; set; }
        }
    

    not sure if this is what you want. this way you get a collection of subitems. 10 subitems of each parent. you can access the parents with the .Parent property of each subitem…

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

Sidebar

Related Questions

I'm using nested set model for my menu tree, and I'm trying to get
I'm using the nested set model that'll later be used to build a sitemap
I've got a Nested Set Model working for my site with items in subcategories
I am using the nested set model to store a large hierarchy of data
I've been using the crap out of the Nested Set Model lately. I have
My model is implemented using Doctrine's Nested Set behaviour. It's a simple hierarchical model
I am trying to implement the nested-set model for a sqlite database. So far
I'm using awesome_nested_set for my central website structure. I want to be able to
I'm using a nested set in a MySQL table to describe a hierarchy of
I want to implement an apperance as this article mentioned using nested ListView control.

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.