Copied to clipboard

Flag this post as spam?

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


  • Bex 444 posts 555 karma points
    Sep 07, 2016 @ 12:05
    Bex
    0

    Filtering Nodes and Adding to New List Razor

    Hi

    This is finally my first outing with umbraco 7 and Razor having moved from Umbraco 4 & 6 and to say i'm confused is an understatement!

    So ok, I'm starting out with what I thought was a very simple problem on an existing site but I've fallen at the first hurdle!!

    I have a Partial View Macro File and I want to get all the pages based on a query and populate a list. This is working fine..

    var startNode = Umbraco.Content(startNodeId);
    var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "Group");
    
    <ul>
        @foreach (var item in selection.OrderBy("Name"))
        {           
            <option data-id="@item.Id" value="@item.Name">@item.Name</option>                   
        }
    </ul>
    

    But.. I need to filter this list. There are some nodes that have the same name, and I only need to show one of them so I am trying to add all the nodes to a new list but only if a node with the same name doesn't already exist.

    So I have added this and then am trying to add these to the list.

    var DistinctSelection = Array[]
    @foreach (var item in selection)
        {           
            @if(!DistinctSelection.Any(n=>n.Name==item.Name)){
                @DistinctSelection.Add(item);   }   
        }
    

    I am really not sure if my syntax is correct but when I run it nothing happens. No error, nothing! I just get an empty list.

    My questions are:

    1. Is this anywhere near right?
    2. Why don't I get any feedback as to what's wrong?

    Any help or guidance would be much appreciated!

    Thanks

    Bex

  • Bex 444 posts 555 karma points
    Sep 07, 2016 @ 12:42
    Bex
    0

    I have found a way of doing it!!

    Courtesy of this post:

    https://our.umbraco.org/forum/developers/razor/19020-Is-there-something-like-%27GroupBy%27

    It doesn't involve adding them to a list or array so I'd still actually like to know if thats possible.. but here's how I've done it:

    var startNode = Umbraco.Content(startNodeId);
    var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "ProductGroup");
    
    
    string parentCurrent = "";
    <ul>
        @foreach (var item in selection.OrderBy("Name"))
        {   
    
             if (parentCurrent != item.Name)
                {         
                    <option data-id="@item.Id" value="@item.Name">@item.Name</option>   
                }
             parentCurrent = item.Name;
        }
    </ul>
    
  • Lucio 24 posts 142 karma points
    Sep 07, 2016 @ 12:55
    Lucio
    0

    Hi Bex,

    Have you tried with DistinctBy. Should be cleaner and faster.

    @{
        var startNode = Umbraco.Content(startNodeId);
        var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "ProductGroup");
    }
    
    <ul>
        @foreach (var item in selection.DistinctBy(s => s.Name))
        {   
             <option data-id="@item.Id" value="@item.Name">@item.Name</option>   
        }
    </ul>
    

    Cheers,

    /Lucio

  • Bex 444 posts 555 karma points
    Sep 07, 2016 @ 13:30
    Bex
    0

    Hi Lucio

    I just tried this but this just gives the empty dropdown again. Is there anyway of getting feedback on errors or when something doesnt work?

  • Lucio 24 posts 142 karma points
    Sep 07, 2016 @ 13:45
    Lucio
    0

    Is it working if replace this:

    @{
        var startNode = Umbraco.Content(startNodeId);
        var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "ProductGroup");
    }
    
    <ul>
        @foreach (var item in selection.DistinctBy(s => s.Name))
        {   
             <option data-id="@item.Id" value="@item.Name">@item.Name</option>   
        }
    </ul>
    

    For this:

    var startNode = Umbraco.Content(startNodeId);
    var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "ProductGroup");
    
    <ul>
        @foreach (var item in selection.DistinctBy(s => s.Name))
        {   
             <option data-id="@item.Id" value="@item.Name">@item.Name</option>   
        }
    </ul>
    

    You are not having any compilation error?

  • Lucio 24 posts 142 karma points
    Sep 07, 2016 @ 13:50
    Lucio
    0

    Ups, I have just see that you are working with a dynamic object (Umbraco.Content instead of Umbraco.TypedContent) so you need to use the next:

    var startNode = Umbraco.Content(startNodeId);
    var selection = startNode.Descendants().Where("Visible && DocumentTypeAlias != @0", "ProductGroup");
    
    <ul>
        @foreach (var item in selection.DistinctBy("Name"))
        {   
             <option data-id="@item.Id" value="@item.Name">@item.Name</option>   
        }
    </ul>
    

    Sorry about that xD

  • Bex 444 posts 555 karma points
    Sep 07, 2016 @ 14:57
    Bex
    0

    Still give blank dropdown with no error :( Least i got it working. Really not liking these no errors. Am I doing this wrong?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 07, 2016 @ 21:38
    Alex Skrypnyk
    0

    Hi Bex and Lucio,

    Problem that you are using dynamic types, if you will rewrite it to strongly typed - you will see errors and it will be easier to work with it.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft