Copied to clipboard

Flag this post as spam?

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


  • Simon 4 posts 95 karma points
    Feb 22, 2018 @ 12:21
    Simon
    0

    Querying on a IEnumberable custom property

    Hello,

    I am trying to create a simple query/filter based on categories. I need to work out how to query multinode tree picker properties or if I should be using Examine instead, its a small site - I expect no more than 50 nodes to query against.

    I have category doc types stored at root

    Categories
    - Cat 1
    - Cat 2

    Article doctype has a multinode tree picker (alias = "newsCategory") to select categories.

    I know the code below won't work, so any pointers on how to query a multinode treepicker proeprty?

    var categoryId = 1142;
    var items = Model.Children<NewsArticle>()
                Where(x => x.NewsCategory.Equals(categoryId));
    

    Thanks Simon

  • Dan Diplo 1505 posts 5911 karma points MVP 4x c-trib
    Feb 22, 2018 @ 14:28
    Dan Diplo
    100

    OK, so on Article you first need to get your categories from MNTP. This will return them as a collection of IPublishedContent.

    So this will get your categories:

    var categories = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("newsCategory");
    

    (I can see you are using strongly-typed models but I'm not familiar with them, so I'm doing it the old way. If you have a property on your model that returns IEnumerable<IPublishedContent> then use that instead).

    OK, then you can get all the articles that are in any of the selected categories something like this:

    var items = Model.Children<NewsArticle>().Where(x => categories.Select(c => c.Id).Contains(x.NewsCategory.Id));
    

    I'm assuming NewsArticle.NewsCategory is a single IPublishedContent.

    The basic idea is you want to check if NewsCategory.Id is contained within any of the Ids of your selected categories. We use the Enumerable.Contains extension method.

    I may have messed up the syntax as I don't know your models, but I hope you get the gist.

  • Simon 4 posts 95 karma points
    Feb 22, 2018 @ 14:48
    Simon
    0

    Thanks Dan that's great.

    I couldn't quite get that working because of my set up and because i'm using strongly typed models. However, it got my head round what I needed to do. Cheers,

    In the end I found the following code worked

    I'm getting the category ID from a querystring then using the Any() method on the NewsCategory property.

    var category = Request.QueryString["category"];
    var items = Model.Children<NewsArticle>()
                    .Where(x => x.NewsCategory.Any(v => v.Name == category));
    

    Thanks for your help again. Cheers, Simon

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies