Copied to clipboard

Flag this post as spam?

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


  • coodey 5 posts 35 karma points
    Sep 28, 2014 @ 00:36
    coodey
    0

    This might be a very simple issue, but i'm new to Umbraco so just trying to get head around it.

    I've managed to create some custom templates which are based off the NewOverview to NewsItem relationship.

    In the overview page i'm wanting to select all Item pages and group by a custom field I have set up. I've added the following into my page:

    @{

      var groupedItems = CurrentPage.Children.GroupBy("Type");

      foreach (var group in groupedItems)

      {

     

    @group.Key

     

      }

    }

    However when I run this I get the following error message: 

    'object' does not contain a definition for 'Key'..

    I've used the debug tool in visual studio and can see that @group definitely has the two properties Key which countains my value and Element, but I can't seem to do anything with either.  Can some please help?

  • coodey 5 posts 35 karma points
    Oct 02, 2014 @ 13:53
    coodey
    0

    Can anybody help?

  • Glen Kelley 3 posts 73 karma points
    Oct 22, 2014 @ 11:52
    Glen Kelley
    101

    Hi coodey,

    I had this same issue and just deicded to work around it as I couldn't see what was causing it (like you say the Umbraco.Web.Dynamics.Grouping object clearly has a Key and Elements property so should work). In the end I created a list of the property I wanted to group by and then lopped through that doing a Children.Where() on the property/value.

    Another issue I experienced was trying to get that list of property values to only go through distinct valus - even including @using System.Linq the view would always give an error about there being no Distinct() on List- very odd, so I added a "dealwith" list to act as a filter.

    It isn't the prettiest solution but it works for grouping content under headers.

    @{
      List<string> dealtWith = new List<string>();
      var categories = CurrentPage.Children().Distinct("category").Pluck("category");
      foreach (var category in categories) {
        if(dealtWith.Contains(category)) { continue; }
        var children = CurrentPage.Children().Where("Category == \""+category+"\"");
        <h3>@category (@children.Count())</h3>
        foreach (var child in children) {
          <h4>@child.Question</h4>
          <div>@child.Answer</div>
        }
        dealtWith.Add(category);
      }
    }
    
  • coodey 5 posts 35 karma points
    Oct 24, 2014 @ 10:07
    coodey
    0

    Thanks for the reply Miku.

    I'll try implementing this tonight and see how it goes, but it looks good. Was a bit frustrating, especially as no-one else seems to have had the problem.

    Cheers

  • coodey 5 posts 35 karma points
    Oct 26, 2014 @ 21:35
    coodey
    0

    This worked brilliantly. Well done. Cheers

  • Scott 95 posts 277 karma points
    Jan 28, 2015 @ 19:18
    Scott
    3

    Hi all, I can see this is not too old a thread.

    I have done it different, look here:

    List<string> categories = CurrentPage.Children.Pluck("category");
    foreach(var category in @categories.Distinct()) {
      @*here you do your magic *@
    }
    

    Kind regards :)

    Scott

Please Sign in or register to post replies

Write your reply to:

Draft