Copied to clipboard

Flag this post as spam?

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


  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 02, 2013 @ 15:58
    bob baty-barr
    0

    simple groupBy based on page property?

    this feels straight forward enough, but obviously, i am missing the point.

    My client has 'Classes' that occur at different times, have different course numbers, but the NAMES are the same - let's call it pageHeading... how would i create a list of all classes, but only show each pageHeading ONCE if they are duplicated?

    i thought this would work.. but no joy

    @{
        // Get root node:
        var root Model.AncestorOrSelf();
        // Get all descendantsfilter by type:
        var nodes root.Descendants("Class");
        // Loop through the filtered nodesdisplaying the properties:
        <ul>
        @foreach (var node in nodes.GroupBy("@node.pageHeading"))
        {
            <li>
                @node.pageHeading
            </li>
        }
        </ul>
    }

    using umbraco 4.8

    thanks!

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Jan 02, 2013 @ 16:09
    Sebastiaan Janssen
    0

    First of all, your foreach should be: 

     

    @foreach (var node in nodes.GroupBy("pageHeading"))

     

    Check out my Razor examples package: http://our.umbraco.org/projects/developer-tools/cultiv-razor-examples

    It has EventOverview which groups on the property EventType, seems similar to what you're doing. As soon as you have a grouping you have to do another foreach in that, the example is: 

    <h2>Event Calendar Grouped By Event Type</h2>
    @{
      var eventsByType = Model.Children.GroupBy("EventType");
    
      foreach (var eventGroup in eventsByType)
      {
        <h3>@library.GetPreValueAsString(eventGroup.Key)</h3>
    
        foreach (var node in Model.Children.Where("EventType == " + eventGroup.Key).OrderBy("EventDateTime"))
        {
          <span>NodeName: @node.Name (Node Id: @node.Id)</span><br />
        }
      }
    }

     

     

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 02, 2013 @ 16:39
    bob baty-barr
    0

    wow, i must be thick -- but i cannot get this to work at all... feels like it should be simple. 

    my node structure looks like this...

    Calendar home

    - semester folder [really just a placeholder]

    - - Course ID# [pageHeading of Unit 1] 

    - - Course ID# [pageHeading of Unit 2]

    - - Course ID# [pageHeading of Unit 1]

    - semester folder

    - - Course ID# [pageHeading of Unit 1]

     

    so, what i need is Unit 1 to only show in the list ONCE, but to show all unique courses below the calendar home.

    does that make sense?

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Jan 02, 2013 @ 16:45
    Sebastiaan Janssen
    100

    The property pageHeading has the value of "Unit 1" or "Unit 2"?

    On the semester folder add this: (let's start with just Children, I know that works, not sure about Descendants):

     

    @{
      var coursesByType = Model.Children.GroupBy("PageHeading");

      foreach (var courseType in coursesByType)
      {
       
    <h3>@library.GetPreValueAsString(courseType.Key)</h3>
     
        foreach (var node in Model.Children.Where("PageHeading == " + courseType.Key))
        {
         
    <span>NodeName: @node.Name (Node Id: @node.Id)</span><br/>
        }
      }
    }

    For Descendants it should be something like: 

    @{
      var coursesByType = Model.Descendants.GroupBy("PageHeading");

      foreach (var courseType in coursesByType)
      {
       
    <h3>@library.GetPreValueAsString(courseType.Key)</h3>
     
        foreach (var node in Model.Descendants.Where("PageHeading == " + courseType.Key))
        {
         
    <span>NodeName: @node.Name (Node Id: @node.Id)</span><br/>
        }
      }
    }

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 02, 2013 @ 16:58
    bob baty-barr
    0

    okay, so the Key thing is what i did not understand... i thought that was a docType property you were referencing... all good now... thanks Sebastiaan!

Please Sign in or register to post replies

Write your reply to:

Draft