Copied to clipboard

Flag this post as spam?

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


  • marie 22 posts 132 karma points
    Sep 20, 2017 @ 20:46
    marie
    0

    How can I remove duplicate values derived from a Multinode Treepicker?

    Using Umbraco version 7.2.8

    I'm attempting to categorize "article" sub-sub pages for a certain template while minimizing the amount of effort for users (i.e., I didn't want anyone to have to repeatedly type out each category for each article). I figured that a Multinode Treepicker would be the easiest option for users.

    I also want to be able to organize the visible list of articles by category into a drop-down menu on the Landing Page. So far, I've been able to get the categories to list properly with this code:

    @{
                    var listHold = CurrentPage.AncestorOrSelf(1).Descendants("ArticleItem");
    
                    foreach (var articleItem in listHold)
                    {
    
                        var categoriesList = articleItem.itemCategories.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        var catCollection = Umbraco.Content(categoriesList);
                        foreach (var cat in catCollection)
                        {
                            <p>@cat.Name</p>
                        }
                    }
                }
    

    But the categories repeat for each article. I only want the category to show up once in the list, no matter how many articles have the same category.

  • Alex Skrypnyk 4696 posts 16804 karma points MVP 2x admin
    Sep 21, 2017 @ 10:30
    Alex Skrypnyk
    0

    Hi Marie

    First of all, do not use dynamics and do not use "Descendants", please read this article - https://our.umbraco.org/documentation/reference/Common-Pitfalls/

    I would remove duplicates like that:

    var listHold = Umbraco.AssignedContentItem.AncestorOrSelf(1).Descendants("ArticleItem");
    
    foreach (var articleItem in listHold)
    {
        var categoriesList = articleItem.GetPropertyValue<string>("itemCategories").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Distinct();
        var catCollection = Umbraco.TypedContent(categoriesList);
        foreach (var cat in catCollection)
        {
            <p>@cat.Name</p>
        }
    }
    

    Distinct() method should help

    Thanks,

    Alex

  • marie 22 posts 132 karma points
    Sep 21, 2017 @ 13:48
    marie
    0

    Hi Alex,

    I still have duplicates showing in my list. I have tried virtually every permutation of "Distinct" -- at virtually every possible location within my code -- I could find through searching for this answer, but none of them seems to work.

    If changing the Descendents or Dynamics usage might fix this, please let me know. Right now, I'm only concerned with making the correct content appear and this method is the only way I've found that does that.

  • Kevin Jump 1191 posts 7192 karma points MVP 2x c-trib
    Sep 21, 2017 @ 14:08
    Kevin Jump
    100

    Hi

    I think the Distinct call would work if you just had one list to get ids from but - I think you need to hold the list of ids in a separate list while you loop through each article and then remove duplicates before you show them.

    var listHold = Umbraco.AssignedContentItem.AncestorOrSelf(1).Descendants("ArticleItem");
    
    var allCategories = new List<string>();
    
    foreach (var articleItem in listHold)
    {
        var categoriesList = articleItem.GetPropertyValue<string>
        ("itemCategories").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Distinct();
    
        allCategories.AddRange(categoriesList);
    }
    
    var catCollection = Umbraco.TypedContent(allCategories.Distinct());
    foreach (var cat in catCollection)
    {
        <p>@cat.Name</p>
    }
    

    this is two loops the first gets the unique ids across all the properties, and the second displays them. you could probably do something clever in the AddRange() call to only add the ones that aren't already in the list, but for simplicity, I've left it adding duplicates and you removing them on the second loop.

  • marie 22 posts 132 karma points
    Sep 21, 2017 @ 14:47
    marie
    0

    That works! Thank you, Kevin!

  • Laurens Gregoir 172 posts 674 karma points
    1 week ago
    Laurens Gregoir
    0

    Apologies for dragging this very old cow from the moat but I have similar issue for removing duplicates from my tags.

    In case someone can shed a light. This is my topic. https://our.umbraco.com/forum/templates-partial-views-and-macros/93507-umbraco-tags-filter-out-multiple-tags-that-lead-to-the-same-page

    Thanks in advance!

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
Please Sign in or register to post replies

Write your reply to:

Draft