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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:34:48+00:00 2026-05-19T00:34:48+00:00

I want to retrieve the data and and display it in sorted (child below

  • 0

I want to retrieve the data and and display it in sorted (child below it’s parent).
The data items defined like this: ID | Title | Parent-ID

What I do is first retrieving all items and then sorting.
Is there a better way to do that with linq?

protected void Page_Load(object sender, EventArgs e)
{          
    List<Category> list2 = new List<Category>();
    ContModel modeltx = new ContModel();

    var ret = modeltx.Categories.ToList();

     GetCategoryList(0, 0, ret, list2);
     string str="";

     foreach (Category cat in list2)
     {
          str=str+cat.Title+"\n";
         TextBox1.Text = str;
     }       
}


   private void GetCategoryList(int iCurID, int iDepth, List<Category> li, List<Category> newList)
    {
        Category tmp;
        string strOffset = "";

        foreach (Category cat in li)
        {
            if ((cat.ParentId) == iCurID)
            {
                for (int i = 1; i <= iDepth; i++)
                    strOffset = strOffset + "-";  

                strOffset = strOffset + cat.Title;

                tmp = cat;
                tmp.Title = strOffset;
                newList.Add(tmp);

                strOffset = "";
                GetCategoryList(cat.CategoryID, iDepth + 1, li, newList);
            }
        }
    }

Update:

How to be if the size of data is huge and i want to use paging?
I can’t Page (.Skip(PageSize * PageIndex).Take(PageSize)) before sorting …

  • 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-19T00:34:49+00:00Added an answer on May 19, 2026 at 12:34 am

    I’m afraid you’ll have to recurse over the LINQ results in your code. Depending on table size and structure you may want to pull down the whole table into memory (as it seems you are doing) and do the hierarchy walk there. If you have a very large table, you may want to make repeated trips to the DB.

    For more information read this great article: Storing Hierarchical Data in a Database

    Your code is already doing the flattening in memory. I would suggest using Skip(pSize * pIndex).Take(pSize) on your list2 object (which contains the flattened) data. Be aware that this may lead to page breaks deep in the hierarchy.

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

Sidebar

Related Questions

Hii, I want to retrieve data from sql database and display dynamically in div
I want to display the data retrieved from the JSP page in my List.
I have DropDownList with CascadingDropDown extender. If I want to retrieve data from the
I am using MySQL as DB and I want to retrieve table data to
I want applications to share certain database data. I want one application to retrieve
I have a HTML table as follows. I want retrieve row values from this
I am a beginner at Jquery. What i want is to display the data
I have some text data from database that I want to display it on
I am using data grid view in Windows Form standalone application to display items
we had a table called data in database and we want to retrieve and

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.