Copied to clipboard

Flag this post as spam?

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


  • utilityLA 7 posts 96 karma points
    Jan 20, 2024 @ 00:33
    utilityLA
    0

    filtering nodes by UrlSegment instead of ID

    I have a 'story category' document type and a 'story' document type. Users can attach story categories to created stories via Contentful Data List property name 'categories'.

    I'm outputting these stories via a Block Grid element. By default, all stories will show up. But if the url has a query parameter (Ex: ?cat=url-segment), then I'd like to output a filtered view where the 'cat' parameter matches stories that have story category with a matching UrlSegment. I'm not receiving any errors, but nothing is outputting however when I using the following code.

    I'm brand new to Umbraco and .net core, so I appreciate what a great resource this community has been so far.

    var currentPage = HttpContextAccessor.HttpContext.Request.Query["cat"];
    
    var stories = 
        Umbraco.ContentAtRoot().DescendantsOrSelfOfType("Story")
            .Where(x => x.IsVisible())
            .Where(x => x.HasValue("categories") && x.Value<IEnumerable<IPublishedContent>>("categories")
            .Select(y => y.UrlSegment)
            .Equals(currentPage))
            .OrderByDescending(x => x.Value<DateTime>("date"))
            .Take(10);
    

    If I use the story category ID as the cat parameter instead of UrlSegment and change the code to the following, it works. But I would prefer to match by UrlSegment if possible.

    Select(y => y.Id)
            .Contains(currentPage))
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jan 20, 2024 @ 16:45
    Marc Goodson
    100

    Hi utiilityLA

    When you have the working filtering your code is

    Select(y=>y.Id)

    this would result in an IEnumerable of Ids

    and your contains clause checks if it contains the current Id

    ...

    but in your example for UrlSegment

    you have

    .Select(y => y.UrlSegment)

    wouldn't that result in an IEnumerable of strings for each UrlSegment?

    and so that would never equal a string category?

    also wondering if Any is what you need? I'm typing this on a phone but something like this:

     Umbraco.ContentAtRoot().DescendantsOrSelfOfType("Story")
            .Where(x => x.IsVisible() && x.HasValue("categories") && x.Value<IEnumerable<IPublishedContent>>("categories")
            .Select(y => y.UrlSegment).Any(f=>f.Equals(currentPage))
            .OrderByDescending(x => x.Value<DateTime>("date"))
            .Take(10);
    

    regards

    marc

  • utilityLA 7 posts 96 karma points
    Jan 20, 2024 @ 19:26
    utilityLA
    0

    Thanks Mark for your response. It makes sense that an IEnumerable of strings would not match a single string.

    Your suggestion of using "Any" worked great! Thank you for pointing me in the right direction.

Please Sign in or register to post replies

Write your reply to:

Draft