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 09, 2013 @ 20:49
    bob baty-barr
    0

    GroupBy and inGroupsOf() question

    I would like to break a list of unique nodes grouped by the pageHeading property, but when i try to use inGroupsOf(4) in the for each i get an error:

    'umbraco.MacroEngines.DynamicGrouping' does not contain a definition for 'InGroupsOf'

    here is my code...

    @{
      int i=1;
      string filterTxt "Filter";
      // Get root node:
      var root Model.AncestorOrSelf();
      
      var coursesByType root.Descendants("Class").Where("!isHoliday").GroupBy("pageHeading");
      foreach (var courseType in coursesByType.InGroupsOf(4))
      {
       filterTxt "Filter" i;
       var isChecked Request.QueryString[filterTxt];
       if (isChecked != null){
                              isChecked "checked = 'checked'";
                              }
       <div style="width: 20%; float: left; margin-right: 3%;">
       <label><input type="checkbox" name="@filterTxt" @isChecked value="@courseType.Key"@courseType.Key</input></label>
       </div>
     i+
      }
      
    }

     

    Any thoughts or help would be greatly appreciated.

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 10, 2013 @ 18:08
    Jason Prothero
    0

    Hey Bob!

    I don't think you need the InGroupsOf().  Its actually already grouped from the GroupBy() above.

    I think you want to do something like this:

     

     var coursesByType = root.Descendants("Class").Where("!isHoliday").GroupBy("pageHeading"); foreach (var courseType in coursesByType) { <h2>@courseType.Key</h2> foreach(var item in courseType.Elements) { <p>@item.Name</p> } }

    Its hard for my to quite determine what markup you need, but does that help see how it would work?

    -Jason

     

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 10, 2013 @ 18:11
    Jason Prothero
    0

    Wow, that didn't work out.

    Trying again

    var coursesByType = root.Descendants("Class").Where("!isHoliday").GroupBy("pageHeading");
    foreach(var
    courseTypeincoursesByType)
    {
    <h2>@courseType.Key</h2>
    foreach(var item in courseType.Elements)
    {
    <p>@item.Name</
    p>
    }
    }

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

    right, but i want to create vertical columns of 4 checkboxes -- thats is what the inGroupsOf was supposed to achieve... does that make sesne? i might be able to use the inGroupsOf(4) on the second for-each

     

    stay tuned?

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 10, 2013 @ 18:59
    bob baty-barr
    0

    okay, so... it appears that i cannot use groupBy and InGroupsOf together... this works...

    @{
      int i=1;
      string filterTxt "Filter";
      string isChecked null;
      // Get root node:
      var root Model.AncestorOrSelf();
      
      var coursesByType root.Descendants("Class").Where("!isHoliday");
    foreach (var item in coursesByType.InGroupsOf(4)){
       <div style="width: 20%; float: left; margin-right: 3%;">
          <ul>
      @foreach (var courseType in item)
      {
       filterTxt "Filter" i;
       isChecked Request.QueryString[filterTxt];
       if (isChecked != null){
                              isChecked "checked = 'checked'";
                              }
       
       <li><label><input type="checkbox" name="@filterTxt" @isChecked value="@courseType.pageHeading"@courseType.pageHeading</input></label></li>
       
     i+
      }
         </ul
      </div>
    }

    }

    however... it does not remove the duplicates...


    any thoughts?

     

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 10, 2013 @ 19:20
    Jason Prothero
    0

    What should it look like ideally?

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 10, 2013 @ 19:22
    bob baty-barr
    0

    ideally, Unit 1 [or any pageHeading that is duplicated] should ONLY appear once [as it would if i could use GroupBy("pageHeading") ;) but alas, i cannot figure out how to use both together :(

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 10, 2013 @ 20:10
    Jason Prothero
    0

     

    How about doing two loops?  

    One to remove duplicates and one to group into columns of 4.

    Something like this: (not tested)

    @{
      int i=1;
      string filterTxt = "Filter";
      // Get root node:
      var root = Model.AncestorOrSelf();

      

      DynamicNodeList uniques = new DynamicNodeList()

      

      // Filter out duplicates by grouping by pageHeading
      var coursesByType = root.Descendants("Class").Where("!isHoliday").GroupBy("pageHeading");
      foreach (var courseType in coursesByType)
      {
       uniques.Add(courseType.Elements[0])
      }

      

      // Group into columns of 4
       foreach (var item in uniques.InGroupsOf(4)){
       <div style="width: 20%; float: left; margin-right: 3%;">
          <ul>
      @foreach (var courseType in item)
      {
    filterTxt = "Filter" + i;
       isChecked = Request.QueryString[filterTxt];
       if (isChecked != null)
       {
       isChecked = "checked = 'checked'";
        }

       

        <li><label><input type="checkbox" name="@filterTxt" @isChecked value="@courseType.pageHeading"> @courseType.pageHeading</input></label></li>

       

     i++; 
      }
         </ul> 
      </div>
      }

      

    }
  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 14, 2013 @ 15:51
    bob baty-barr
    0

    okay, i am tinkering with the above... and i get the following error and have no idea what it means...

    The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.InGroupsOf(int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    any ideas?

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 14, 2013 @ 18:28
    Jason Prothero
    0

    Perhaps this would work?

     

    uniques.InGroupsOf<DynamicNode>(4);
  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 14, 2013 @ 18:32
    bob baty-barr
    0

    grrr.... foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

    so sorry that this is being such a pain in the butt for both of us... :(

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 14, 2013 @ 18:41
    Jason Prothero
    0
    @foreach(DynamicNode courseType in item)

    Maybe this change?  What line is it complaining about?

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 14, 2013 @ 18:46
    bob baty-barr
    0

    it has to do with groupsOf and that dynamic node stuff ;)

    \listUnits.cshtml(30): error CS0411: The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.InGroupsOf<T>(int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 14, 2013 @ 18:51
    bob baty-barr
    0

    actually... if i pull out the inGroupsOf() logic... just building the dynamicNodeList... i get this error... so somethign not correct here either.

    ) Cannot apply indexing with [] to an expression of type 'object'    at CallSite.Target(Closure , CallSite , Object , Int32 )

     

    uniques.Add(courseType.Elements[0]);
  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 14, 2013 @ 19:07
    Jason Prothero
    0

    Yea, I just wrote the code in the RTE quickly.  No testing done.  Could be that the [0] is actually not correct.

    I'll try to setup a test environment if I get a chance.

     

    Thanks,

    Jason

  • Jason Prothero 422 posts 1243 karma points c-trib
    Jan 28, 2013 @ 19:15
    Jason Prothero
    0

    We're you able to figure this one out?

     

    -J

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jan 28, 2013 @ 19:54
    bob baty-barr
    0

    i ended up using MOD operator % -- not sure if that is hacky or not... but basically, i don't think groupBy and inGroupsOf can work together :(

  • Jason Prothero 422 posts 1243 karma points c-trib
    Feb 05, 2013 @ 17:15
    Jason Prothero
    0

    Bummer.  It did seem a bit like that.  Well, glad you found a solution!

     

    -Jason

Please Sign in or register to post replies

Write your reply to:

Draft