Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>using the link <a href="http://blogs.msdn.com/b/mitsu/archive/2007/12/22/playing-with-linq-grouping-groupbymany.aspx" rel="nofollow noreferrer">Playing with Linq grouping: GroupByMany ?</a> suggested in <a href="https://stackoverflow.com/questions/2230202/how-can-i-hierarchically-group-data-using-linq">How can I hierarchically group data using LINQ?</a></p> <p>I first refactored my code into </p> <p><strong>Solution 1</strong></p> <pre><code>var results = from allPosts in db.Posts.OrderBy(p =&gt; p.DateCreated) group allPosts by allPosts.DateCreated.Year into postsByYear select new { postsByYear.Key, SubGroups = from yearLevelPosts in postsByYear group yearLevelPosts by yearLevelPosts.DateCreated.Month into postsByMonth select new { postsByMonth.Key, SubGroups = from monthLevelPosts in postsByMonth group monthLevelPosts by monthLevelPosts.Title into post select post } }; foreach (var yearPosts in results) { //create "year-level" item var year = new BlogEntry { Name = yearPosts.Key.ToString().ToLink(string.Empty) }; entries.Add(year); foreach (var monthPosts in yearPosts.SubGroups) { //create "month-level" item var month = new BlogEntry { Name = new DateTime(2000, (int)monthPosts.Key, 1).ToString("MMMM").ToLink(string.Empty), Parent = year }; year.Children.Add(month); foreach (var postEntry in monthPosts.SubGroups) { //create "blog entry level" item var post = postEntry.First() as Post; var blogEntry = new BlogEntry { Name = post.Title.ToLink("/Post/" + post.PostID + "/" + post.Title.ToSeoUrl()), Parent = month }; month.Children.Add(blogEntry); } } } </code></pre> <p>And then into a more generic</p> <p><strong>Solution 2</strong></p> <pre><code>var results = db.Posts.OrderBy(p =&gt; p.DateCreated).GroupByMany(p =&gt; p.DateCreated.Year, p =&gt; p.DateCreated.Month); foreach (var yearPosts in results) { //create "year-level" item var year = new BlogEntry { Name = yearPosts.Key.ToString().ToLink(string.Empty) }; entries.Add(year); foreach (var monthPosts in yearPosts.SubGroups) { //create "month-level" item var month = new BlogEntry { Name = new DateTime(2000, (int)monthPosts.Key, 1).ToString("MMMM").ToLink(string.Empty), Parent = year }; year.Children.Add(month); foreach (var postEntry in monthPosts.Items) { //create "blog entry level" item var post = postEntry as Post; var blogEntry = new BlogEntry { Name = post.Title.ToLink("/Post/" + post.PostID + "/" + post.Title.ToSeoUrl()), Parent = month }; month.Children.Add(blogEntry); } } } ................................................ public static class MyEnumerableExtensions { public static IEnumerable&lt;GroupResult&gt; GroupByMany&lt;TElement&gt;( this IEnumerable&lt;TElement&gt; elements, params Func&lt;TElement, object&gt;[] groupSelectors) { if (groupSelectors.Length &gt; 0) { var selector = groupSelectors.First(); //reduce the list recursively until zero var nextSelectors = groupSelectors.Skip(1).ToArray(); return elements.GroupBy(selector).Select( g =&gt; new GroupResult { Key = g.Key, Items = g, SubGroups = g.GroupByMany(nextSelectors) }); } else return null; } } public class GroupResult { public object Key { get; set; } public IEnumerable Items { get; set; } public IEnumerable&lt;GroupResult&gt; SubGroups { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload