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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:17:31+00:00 2026-06-18T02:17:31+00:00

I’ve written recursive function to retrieve this parent/child style menu from database: <ul> <li>

  • 0

I’ve written recursive function to retrieve this parent/child style menu from database:

<ul>
  <li>
  <a href='#'>level1-a</a>
    <ul> 
      <li>
      <a href='#'>level2-a1</a>
    <ul>
          <li><a href='#'>level3-a11</a></li>
    </ul>
      </li>
      <li><a href='#'>level2-a2</a></li>
    </ul>
  </li>
  <li><a href='#'>level1-b</a></li>
  <li><a href='#'>level1-c</a></li>
</ul>

I know it’s not a good idea to write such function for retrieving data from database with a recursive function, sometimes it takes long time to run.

It’s my algorithm (C# and VB.net code is provided):

my_function (ID){
 WHILE (read_from_table){
   PRINT data
     my_function(child_id)
 }
}

C# code: http://pastebin.com/hsqhYF72
VB.net code: http://pastebin.com/HnyrYnab

Is there any type of variable that is able to store such structure of data to search inside it instead of connecting to a database continually?

  • 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-06-18T02:17:33+00:00Added an answer on June 18, 2026 at 2:17 am

    Ideally, you want to retrieve all the relevant records at once into an in-memory collection, and then read from that memory collection during your recursion. The paradigm of memoization/caching of records will make this simple, as well as separating your data-access logic from business logic.

    First, create a static method for retrieving data that grabs data from the database the first time, but makes use of its in-memory collection on subsequent calls. I’m assuming that since you are passing fTableName, this method may or may not be used with multiple tables, so the cache is capable of storing multiple tables at once (using a Dictionary keyed on the table name), and treats requests to different tables separately. (WARNING: untested code, but should give you the idea):

    private static Dictionary<string, DataTable> _menuCache = null;
    public static DataRow[] GetMenuLayer(string fTableName, string fID, string fClause)
    {
        if (_menuCache == null) _menuCache = new Dictionary<string, DataTable>();
        if (!_menuCache.ContainsKey(fTableName)) { 
            // retrieve all records from the database the first time
            SQLCommand = "SELECT * FROM " + fTableName;
            ...
            _menuCache[fTableName] = result;
        }
    
        // query appropriate records from the cache
        var dt = _menuCache[fTableName];
        return dt.Select("MenuParent = " + fID + " AND Visible=1 AND " + fClause);
    }
    

    Since this method is static, its data is saved during the entire request/response cycle, but not between responses. So this will reduce generating the menu to a single database call, while each time the page is loaded, the data is still loaded new. If your menu data is relatively static, you could go to the next level using the .NET cache with a timeout that will store the database results, for say 30 minutes at a time before refreshing. Then the page could be loaded many times with only a single database call.

    The code in your GenerateNestedMenus that retrieves data from the database would instead make a call to GetMenuLayer with the appropriate parameters. This method then is responsible for retrieving the data in whatever method it can (the former method doesn’t need to care how it gets there). Behind the scenes, the first time a table fTableName is requested, the entire table is downloaded into a local memory cache. Then that memory cache is queried in subsequent calls according to the parameters to return the result rows that can be iterated (this assumes that your dynamic filter logic fClause is not too complicated, because dt.Select( only understands a very small subset of basic T-SQL logic):

    public string GenerateNestedMenus(string fTableName, string fID, string fClause)
    {
    
        DataRow[] dt = GetMenuLayer(fTableName, fID, fClause);
    
        int i = 0;
        string temp = null;
        for (i = 0; i <= dt.Length - 1; i++) {
            if (Convert.ToInt32(ChildCounter(fTableName, dt[i]["id"])) > 0) {
                temp = "<li>" + Constants.vbCrLf + "<a href='#'>" + Strings.Trim(dt[i]["MenuName"]) + "</a>" + Constants.vbCrLf + Constants.vbTab + "<ul> ";
                _temp += temp;
                GenerateNestedMenus2("menus", dt[i]["id"], fClause);
                _temp += Constants.vbTab + "</ul>" + Constants.vbCrLf + Constants.vbTab + "</li>" + Constants.vbCrLf;
            } else {
                //For rows they have not child
                temp = Constants.vbTab + "<li><a href='#'>" + Strings.Trim(dt[i]["MenuName"]) + "</a>" + "</li>" + Constants.vbCrLf;
                _temp += temp;
                GenerateNestedMenus2("menus", dt[i]["id"], fClause);
    
            }
        }
        return _temp;
    }
    

    This is a rough idea of the approach to a solution, which may require some tweaking and experimenting to get everything working.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.