Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Jan 20, 2016 @ 23:13
    Amir Khan
    0

    Access property after grouping

    Hi!

    I'm grouping a list of items by a date type property, which is working great. However, inside the initial group, I'm looking to add a header with the day of the group, I can't seem to access any properties of the group.

    Any ideas (code below)?

    Thanks!

    Amir

    @foreach(var itemGroup in selection.OrderBy("startDateAndTime").Skip((page -1)* pageSize).Take(pageSize).GroupBy("startDateAndTime.Day")) {
    
    <h1>@itemGroup.startDateAndTime.Day</h1> how do i get the date here!?
    
    foreach(var item in itemGroup) {
      @item.Name
    }
    

    }

  • Yasir Butt 161 posts 371 karma points
    Jan 20, 2016 @ 23:20
    Yasir Butt
    0

    what is selection? is selection ipublishedcontent? then you can "itemgroup.GetpropertyValue< DateTime >("startdate").Day"

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jan 21, 2016 @ 08:54
    Dave Woestenborghs
    0

    You can do this I think :

    <h1>@itemGroup.Key</h1> how do i get the date here!?
    

    Haven't actually tried this code, but should work

    Dave

  • Amir Khan 1287 posts 2744 karma points
    Jan 21, 2016 @ 19:05
    Amir Khan
    0

    Yasir, selection is Umbraco.Web.Models.DynamicPublishedContentList

    itemGroup is Umbraco.Web.Dynamics.Grouping`2[System.Object,Umbraco.Web.Models.DynamicPublishedContent]

    Unfortunately both of the above suggestions return " 'object' does not contain a definition for..."

    Any ideas?

    -Amir

  • Amir Khan 1287 posts 2744 karma points
    Jan 21, 2016 @ 20:24
    Amir Khan
    0

    Okay, changing selection to iPublishedContent by using Model.Content instead of CurrentPage allowed me to get @itemGroup.Key to work, but I still can't access any other properties? I can only get the group by day and not the year.

    Is there a way to access the properties of the group or even an item in it?

    Thanks!

    Amir

  • Yasir Butt 161 posts 371 karma points
    Jan 21, 2016 @ 20:34
    Yasir Butt
    0

    Have you tried itemgroup.GetpropertyValue("yourproperty")?

  • Amir Khan 1287 posts 2744 karma points
    Jan 21, 2016 @ 20:35
    Amir Khan
    0

    Yep, get this: CS1928: 'System.Linq.IGrouping

  • Yasir Butt 161 posts 371 karma points
    Jan 21, 2016 @ 21:02
    Yasir Butt
    0

    why are you using groupby? can you not use this without groupby?

  • Amir Khan 1287 posts 2744 karma points
    Jan 21, 2016 @ 21:36
    Amir Khan
    0

    I want to group items by day, add a heading for each day above the items, then iterate through the items. I want to heading to have day/month, hence the need to access more than just the key.

    -Amir

  • Yasir Butt 161 posts 371 karma points
    Jan 21, 2016 @ 21:47
    Yasir Butt
    0

    ok!

    it can be achieve like this.

    @foreach(var itemGroup in selection.OrderBy("startDateAndTime").Skip((page -1)* pageSize).Take(pageSize)) { 
    var group = new DateTime();
    if(group!=itemGroup.GetPropertyValue<DateTime>("startDateAndTime")){
    <h1>@itemGroup.startDateAndTime.Day</h1> 
    }
    group = itemGroup.GetPropertyValue<DateTime>("startDateAndTime");
    @itemGroup.Name
    
  • Amir Khan 1287 posts 2744 karma points
    Jan 25, 2016 @ 15:50
    Amir Khan
    0

    Hi Yasir, the grouping here just pulls the day for each item, not for the group.

    I'm trying to actually access properties of the group like:

    Group Date - group item - group item - group item

    Group 2 Date - group 2 item - group 2 item - group 2 item

    Is there a way maybe to just insert something before the first item in the group? -Amir

  • Amir Khan 1287 posts 2744 karma points
    Jan 25, 2016 @ 15:59
    Amir Khan
    0

    Inserting something before the first item worked perfectly.

    Here's what I ended up with:

    @foreach(var itemGroup in selection.OrderBy("startDateAndTime").Skip((page -1)* pageSize).Take(pageSize).GroupBy("startDateAndTime.Day")) {
    foreach(var item in itemGroup) {
      if(item.IsFirst()) {
        <div class="grid-row-scheme-teal smallPadding marginBottom">
          @{
            string date = @item.GetPropertyValue("startDateAndTime").ToString();
          <h1>@umbraco.library.FormatDateTime(date, "MM/d ")</h1> 
          }
        </div>     
      }
    
  • Yasir Butt 161 posts 371 karma points
    Jan 25, 2016 @ 20:31
    Yasir Butt
    0

    ok! have you solved it?

    Now i noticed one thing in my code

    if you just put the

    var group = new DateTime();
    

    Before loop then it should work as you want to.

Please Sign in or register to post replies

Write your reply to:

Draft