Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vincent Baaij 95 posts 344 karma points c-trib
    Mar 31, 2011 @ 09:48
    Vincent Baaij
    0

    Is there something like 'GroupBy'

    Hi,

    I have assembled a DynamicNodeList with items from different places in the site structure. Now I want to output them grouped as <ul /> list with a header that contais the parent name/title of the items. Anyone got a solution for this?

    Sample list:

    item 1 (parent a), item 2 (parent b), item 3 (parent b), item 4 (parent b), item 5 (parent a)

    Output:

    Parent a
       item 1
       item 5
    Parent b
      item 2
      item 3
      item 4

     

  • Alex 78 posts 136 karma points
    Mar 31, 2011 @ 10:17
    Alex
    0

    Not sure if GroupBy is supported or not, but you could probably do something like this (not tested!) to get round it, where list is your DynamicNodeList

        @{
          string parentCurrent = "";
          foreach(dynamic node in list.OrderBy("Parent.Name"))
          {
            if (parentCurrent != node.Parent.Name)
            {
              if (parentCurrent != "")
              {
                @:</ul>
              }
              @node.Parent.Name
              @:<ul>        
            }
            <li>@node.Name</li>
            parentCurrent = node.Parent.Name;
          }
          @:</ul>
        }

     

  • Vincent Baaij 95 posts 344 karma points c-trib
    Mar 31, 2011 @ 11:04
    Vincent Baaij
    0

    @Alex: Thanks.

    The code I had so far did almost the same (appearantly I 'm not that dumb after all :)) Key to get it working was in the use of @:

  • Carlos 338 posts 472 karma points
    Jun 21, 2012 @ 21:09
    Carlos
    0

    Now that there is GroupBy in Umbraco. How do you do that>

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 21, 2012 @ 21:17
    Dan Diplo
    0

    Check the Razor Cheat Sheet for syntax...

  • Carlos 338 posts 472 karma points
    Jun 21, 2012 @ 21:22
    Carlos
    0

    I did. I have them Grouped. However, here is my dillema, I am getting multiple nodes with the same CreatorNames.  I only want the Creator names to show once in my list, not to show up for each node they have authored.  

    More or less I am combining a list of Creator Names from multiple nodes and want them all in one list.  But I only want their name to show up once in the list. Not multiple times as they do now when I use the Groupby and ForEach. 

    My code is below.

    <h3>@Dictionary.DWTAuthors</h3>
      <ul>
      @foreach(var item in @Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").GroupBy("CreatorName")){
           foreach(var node in @item{                                                                             
          <li>@node.CreatorName</li
               }                                                                                                    
       }
      </ul>  

     

     

    Thanks in advance.

  • Carlos 338 posts 472 karma points
    Jun 21, 2012 @ 21:29
    Carlos
    0

    Well, I found my solution.  I picked apart the example from above. If you are interested.

     

    string parentCurrent "";
          foreach(dynamic node in @Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").OrderBy("CreatorName"))
          {
            if (parentCurrent != node.CreatorName)
            {
              <li>@node.CreatorName</li>    
            }
            parentCurrent node.CreatorName;
          }
          @:</ul>

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 21, 2012 @ 21:47
    Dan Diplo
    2

    So you actually want a distinct list of CreateNames? You probably want Disinct() but this isn't easy to implement. So, you can "hack it" using a bit of LINQ and GroupBy...

    var allNodes = Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").OrderBy("CreatorName");
    var distinct = allNodes.Items.GroupBy(x => x.CreatorName).Select(grp => grp.First());
  • Scott 95 posts 277 karma points
    Jan 29, 2015 @ 12:37
Please Sign in or register to post replies

Write your reply to:

Draft