Copied to clipboard

Flag this post as spam?

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


  • CamT 5 posts 85 karma points
    Aug 22, 2018 @ 11:23
    CamT
    0

    Finding Related Items

    Hi Everyone,

    Pretty new to Umbraco and found this forum super helpful so far but this is the first time I've not fould a solution in a previously asked question.

    What I have is a list of companies that have a category propery each can have mulitple categories. I'm trying to get a list of similar companies so if Bloggs & Co are in Cat1, Cat3 and Cat5 I would like a list of any other companies that are also in any of those categories.

    Now the code below does give me that list however returns duplicates if the company is found in more than one of the categories which is often the case.

    Am I missing an obvious way to return a distinct list or have I gone about it in totally the wrong way?

    @{
            var categoryFilter = Category1, Category2, Category7";
    
            var selection = CurrentPage.Site().FirstChild("exhibitorList").Children("exhibitor").Where("Visible");
        }
        <ul>
            @foreach(var item in selection.Distinct().ToArray() ){
                string ScompanyCat=item.categories;  
                foreach(string variable [email protected](',')){
    
            if(ScompanyCat.Contains(variable)){
    
    
                <li>
                    <a href="@item.Url">@item.Name </a> - (@variable) 
    
    
                    </li>
    
    
                }
            } 
    
        }
    

    Any help at all would be much appreciated!

    Thanks :)

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Aug 22, 2018 @ 19:44
    Kevin Jump
    100

    Hi

    with a little switch to typed models from dynamics (mainly using Model.Content instead of CurrentPage) you can do it in a couple of lines (i think - haven't tested this exact code!)

    var categoryFilter = "Category1, Category2, Category7";
    
    // split the filters into a list. 
    var filter = categoryFilter.ToDelimitedList();
    
    var selection = Model.Content.Site().FirstChild("exhibitorList").Children("exhibitor").Where(x => x.IsVisible());
    
    // above can be quite slow consider : 
    // var selection = Umbraco.TypedContentAtXPath("//exhibitor");
    // to get all exhibitors across the site - much quicker.
    
    // check each item, to see if the filter contains any of the cateogries in it (split into a list)
    var exhibitors =
        selection
            .Where(x => filter.ContainsAny(x.GetPropertyValue<string>("categories", "").ToDelimitedList(",")))
            .Distinct();
    

    probably to note that is a case sensitive search , so you would need to make sure everything was the same.

    you could do it without splitting the first list :

    var exhibitors =
        selection
            .Where(x => categoryFilter.ContainsAny(x.GetPropertyValue<string>("categories", "").ToDelimitedList(","), StringComparison.InvariantCultureIgnoreCase))
            .Distinct();
    

    its just a bit longer for the answer?

  • CamT 5 posts 85 karma points
    Aug 22, 2018 @ 20:38
    CamT
    0

    Hi Kevin

    Thanks so much for taking the time to answer, that worked perfectly for me.

    Dread to think how long I'd spent trying to figure this out!

    Thanks again Cameron

Please Sign in or register to post replies

Write your reply to:

Draft