Copied to clipboard

Flag this post as spam?

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


  • Thomas 66 posts 88 karma points
    Jan 30, 2012 @ 12:03
    Thomas
    0

    check if Node is included in DynamicNodeList

    I have a very simple issue (it seems) - Id like to get distinct parent nodes from a DynamicNodeList.

    I have no trouble creating a new DynamicNodeList with the parent nodes, but want to get rid of the duplicates. Distinct on Items throws no errors, but I suspect that dynamic nodes are all distinct.

    So the solution seems to be only to add items that doesn't already exists in the DynamicNodeList:

    @{
          DynamicNodeList uniqueList = new DynamicNodeList();
        }
        <ul>
        @foreach (DynamicNode item in followedNodes) {  
          if (uniqueList.Items.Where("Id == item.Id").Count() 1) {
            uniqueList.Add(item.Parent);     
          }
        }
     
        @foreach (DynamicNode item in uniqueList.Items.Distinct()) {
          <li>
            <href="@item.Url">@item.Name</a
          </li>
        }
        </ul>  

    The above seems clumsy and throws an error with Where:'System.Collections.Generic.List' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments

    Any ideas?

     

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 13:17
    Rodion Novoselov
    0

    I guess it's still far from ideal but a bit shorter solution:

    <ul>
        @foreach(var g in followedNodes.GroupBy("Parent.Id"))
        {
           var p = ((IEnumerable<DynamicNode>)g).First().Parent;
           <li><a href="@p.Url">@p.Name</a></li>
        }
    </ul>

     

  • Thomas 66 posts 88 karma points
    Jan 30, 2012 @ 13:46
    Thomas
    0

    Groupby and taking the first of each seems clever. It does however return this error:

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

    Is it possible to use GroupBy on a DynamicNodeList or what might be wrong?

     

    Just an idea: do you think it would be possible just to get the Ancestors() of the DynamicNodeList? It did throw an error, but seems like a very simple solution to the problem.

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 14:34
    Rodion Novoselov
    0

    That's strange - I tried that code and it worked. Ok, perhaps I'll proceed with some investigation a bit later today :)

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 15:09
    Rodion Novoselov
    0

    Well, I got it. You're trying to call the GroupBy method on the object casted to "DynamicNodeList". To work as expected it should be called strictly on a dynamic object. Cast your "followedNodes" to "dynamic" and everything will be all right. 

    PS: I often get confused about dynamics as well...

  • Thomas 66 posts 88 karma points
    Jan 30, 2012 @ 17:07
    Thomas
    0

    Something went wrong here - I don't know what but didn't get i working - Instead i created an oldschool list of Node Id's and used Distinct(), which worked out well. Have to look closer into the issue at a later stage.

    Thanks for your effort Rodion :o)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies