Copied to clipboard

Flag this post as spam?

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


  • Björn 18 posts 158 karma points
    Feb 26, 2018 @ 14:01
    Björn
    0

    Select articles which contains a certain category

    Hi, I would like some help on how to write a linq query which retrieves articles of a certain category. I've built a site that contains pages, news and category. A page can have on and only one category apointed to it while a newsitem can have several categories appointed to it. When on a page, I would like to retrieve the newsitems which have the same category as the page. How do I go about doing that?

    Both the page and newsitems uses Umbracos multinode treepicker to select categories from the category folder. I've managed to retrieve the category for the page and if a newsitem only could have one category assigned to it there wouldn't be any problem. Then I could just use FirstOrDefault():

            newsItems = Umbraco.TypedContentAtXPath("//newsPage").Where(x => x.IsVisible().Equals(true) && x.HasValue("category")).Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("category").FirstOrDefault().GetPropertyValue("categoryName").ToString().Equals(catName)).OrderBy("createDate desc");
    

    But how should it be written when there can be multiple categories?

    Kind regards /Björn

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Feb 26, 2018 @ 16:08
    Steve Morgan
    100

    Hi,

    Try

    newsItems = Umbraco.TypedContentAtXPath("//newsPage").Children().Where(x => (x.GetPropertyValue<IEnumerable<IPublishedContent>>("categories").Where(y => y.Name == catName).Any()));
    

    I've written categories but you might have this as "category"?

    HTH

    Steve

  • Björn 18 posts 158 karma points
    Feb 28, 2018 @ 10:52
    Björn
    0

    Awesome, that's exactly what I needed. Thanks.

    /Björn

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Feb 28, 2018 @ 11:25
    Steve Morgan
    0

    Cool - glad it worked!

Please Sign in or register to post replies

Write your reply to:

Draft