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));
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.
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));
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?
Thanks Simon
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:
(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:
I'm assuming
NewsArticle.NewsCategory
is a singleIPublishedContent
.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.
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.
Thanks for your help again. Cheers, Simon
is working on a reply...