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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:58:55+00:00 2026-06-16T11:58:55+00:00

I couldn’t find or come up with a generic and elegant algorithm that would

  • 0

I couldn’t find or come up with a generic and elegant algorithm that would let me populate the tree-like structure.
The simplest example is a blog archive: I have a bunch of records which can be selected and sorted by date.
I want to have a tree where years may be top level, months are next level and actual post titles are next again.

So far I’ve came up with a naive straight forward implementation that works, but I’m sure that could be improved using LINQ, etc.
Here I just sort records by date and iterate checking if the year or month has changed, and add tree nodes accordingly.
“BlogEntry” is a class that has a reference to both a parent and children and is later used to generate HTML.

I welcome suggestions on improving the algorithm!

IEnumerable<Post> posts = db.Posts.OrderBy(p => p.DateCreated);
var topPost = posts.First();

int curYear = topPost.DateCreated.Year;
int curMonth = topPost.DateCreated.Month;

//create first "year-level" item
var topYear = new BlogEntry { Name = topPost.DateCreated.Year.ToString().ToLink(string.Empty) };
entries.Add(topYear);
var currentYear = topYear;

var topMonth = new BlogEntry { Name = topPost.DateCreated.ToString("MMMM").ToLink(string.Empty), Parent = currentYear };
currentYear.Children.Add(topMonth);
var currentMonth = topMonth;

foreach (var post in posts)
{
    if(post.DateCreated.Year == curYear)
    {
        if (post.DateCreated.Month != curMonth)
        {
            //create "month-level" item
            var month = new BlogEntry { Name = post.DateCreated.ToString("MMMM").ToLink(string.Empty), Parent = currentYear };
            currentYear.Children.Add(month);
            currentMonth = month;

            curMonth = post.DateCreated.Month;
        }

        //create "blog entry level" item
        var blogEntry = new BlogEntry { Name = post.Title.ToLink("/Post/" + post.PostID + "/" + post.Title.ToSeoUrl() ), Parent = currentMonth };
        currentMonth.Children.Add(blogEntry);
    }
    else
    {
        //create "year-level" item
        var year = new BlogEntry { Name = post.DateCreated.Year.ToString().ToLink(string.Empty) };
        entries.Add(year);
        currentYear = year;

        curMonth = post.DateCreated.Month;
        curYear = post.DateCreated.Year;
    }
}
  • 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-16T11:58:56+00:00Added an answer on June 16, 2026 at 11:58 am

    I’ve created a test example, to check the correctness of the logic. I think this is what you need.

    public class BlogEntyTreeItem
    {
       public string Text { set; get; }
       public string URL { set; get; }
       public List<BlogEntyTreeItem> Children { set; get; }
    
       public List<BlogEntyTreeItem> GetTree()
       {
           NWDataContext db = new NWDataContext();
           var p = db.Posts.ToList();
    
           var list = p.GroupBy(g => g.DateCreated.Year).Select(g => new BlogEntyTreeItem
           {
               Text = g.Key.ToString(),
               Children = g.GroupBy(g1 => g1.DateCreated.ToString("MMMM")).Select(g1 => new BlogEntyTreeItem
               {
                   Text = g1.Key,
                   Children = g1.Select(i => new BlogEntyTreeItem { Text = i.Name }).ToList()
               }).ToList()
           }).ToList();
    
           return list;        
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Couldn't find a trace of the source code behind the Photos application that runs
Couldn't find any file-tree component (cross-platform, of course) in the FireMonkey framework. Anyone has
I couldn't find the answer to this in any other question. Suppose that I
Couldn't find a method like the int executeUpdate(String sql, int autoGeneratedKeys) in php. I
Couldn't find similar questions: I'm currently designing my app and after that I will
Couldn't find_if just be an overload of find ? That's how std::binary_search and friends
Couldn't find this in the faq. Does Flurry give analytics that distinguish users? So
Couldn't find an answer to this one. I have a WPF ListView control that
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
I couldn't find anything similar to this anywhere. I have an array of pointers

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.