Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Apr 08, 2022 @ 10:45
    Dibs
    0

    Linq filtering

    Hi Umbraco Team

    I am trying to filter content using Linq, i'm trying to return same doctypes as the current node and with the same value of a checklist property field value. I have tried the following

    @{
        var relatedDoc = Umbraco.ContentAtXPath("//relatedDocs");
    
        foreach (var doc in relatedDoc .Where(x => x.Value("topic").ToString() == (Model.Topic).ToString()))
        {
            <p> @doc.Name</p>
    
        }
    }
    

    and

    @{
        var relatedDoc = Umbraco.ContentAtXPath("//relatedDocs");
    
        foreach (var doc in relatedDoc .Where(x => x.Value("topic").Equals(Model.Topic)))
        {
            <p> @doc.Name</p>
    
        }
    }
    

    The above returns only the current node, I would like to return only the same doctypes with the same property value as the node current node not the current node

    Thanks Dibs

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Apr 12, 2022 @ 06:15
    Marc Goodson
    0

    Hi Dibs

    You say your Topic is a checkbox list?

    Does that mean a page can have multiple topics ticked??

    If so then the stored value for a Topic won't be a 'single' topic, it's more likely to be a comma-separated list of topics? (depends on what the property editor is for the Checkbox list)

    so if you have a topic with three checkboxes ticked

    fishing, boating, fighting

    then with your implementation above, that will only match articles with those three checkboxes ticked in that order.

    eg another topic with only 'fishing' topic checked will not match as

    "fishing" != "fishing,boating,fighting"

    If the editor stores a comma delimited string like this then you would need to turn the string into an array, using String.Split() and then compare the two arrays using 'Intersect'

    var currentArticleTopics = String.Split(Model.Value<string>("topic"), new char[]{','},StringSplitOptions.RemoveEmptyEntries);
    
    foreach (var doc in relatedDoc .Where(x => String.Split(x.Value<string>("topic"), new char[]{','},StringSplitOptions.RemoveEmptyEntries).Intersect(currentArticleTopics).Count() > 0));
    

    Typing on a phone, so fingers crossed that example gives you the gist if what you need, if not necessarily the most perfect way. Depending on the amount of content on your site, then you might find better performance using an Examine search query to look for the related articles.

    regards

    Marc

  • Dibs 202 posts 991 karma points
    Apr 12, 2022 @ 13:25
    Dibs
    0

    Cheers Mark

    for feedback

    I was able to check my checklist to get related nodes/documents by the following

    var relatedDocuments = Umbraco.ContentAtXPath("//Page").Where(x => x.Value<IEnumerable<string>>("topic").SequenceEqual(Model.Topic) && x.Name != Model.Name));
    

    above returned all, except current, nodes of same doctype that had same checklist items selected

    Dibs : )

Please Sign in or register to post replies

Write your reply to:

Draft