I have a node containing FAQ nodes. The FAQs are rendered simply on a page with a partial view, and may be filtered by a category id (or several) passed in via a querystring.
The code I have works nicely, like this:
// Create array of category id(s) from querystring values
string categoryId = Request.QueryString["category"];
categoryId = categoryId.IsNullOrWhiteSpace() ? "" : categoryId;
string[] categoryIds = categoryId.Split(',');
// Get faq nodes
var faqs = @Model.Children.Where("Visible").OrderBy("date");
// Loop through nodes
foreach (var faq in faqs)
{
// Get category(/ies) current node belongs to
var category = faq.GetPropertyValue("category").ToString();
string[] categories = category.Split(',');
// If there's a category filter from querystring, select only nodes in those categories
if (String.IsNullOrEmpty(categoryId) || categories.Any(categoryIds.Contains)){
// Output mark-up here
<p>@faq.Name</p>
}
}
Now though, I want to paginate this. So I really would like to combine the following two lines into a single query if possible:
var faqs = @Model.Children.Where("Visible").OrderBy("date");
and
if (String.IsNullOrEmpty(categoryId) || categories.Any(categoryIds.Contains))
Otherwise it'll mean doing two loops of the node-set, each with a check for categories; one to count the number of records for pagination and another to output the mark-up later on the page.
So essentially the 'if' logic becomes part of the 'where' part of the node selector.
The other alternative is to do one loop and create a temporary variable of nodes and count and loop that, which feels better than two separate loops, but not as ideal as being able to just have a single selector that takes the category filter into account.
Is it possible to do such a selector as part of the node-set gathering? Any pointers much appreciated.
I've not been able to combine this into a single statement unfortunately - it just won't seem to accept the 'or' (||) part of the expression. The closest I've got is this:
However, I'm struggling to declare the correct type on the 'faqs' variable. I can't do var faqs; as "implicitly typed local variables must be initialized" and it errors if I do IPublishedContent faqs; saying "Cannot implicitly convert system.linq.iqueryable
Best way to filter node-set within razor query
Hi,
I have a node containing FAQ nodes. The FAQs are rendered simply on a page with a partial view, and may be filtered by a category id (or several) passed in via a querystring.
The code I have works nicely, like this:
Now though, I want to paginate this. So I really would like to combine the following two lines into a single query if possible:
and
Otherwise it'll mean doing two loops of the node-set, each with a check for categories; one to count the number of records for pagination and another to output the mark-up later on the page.
So essentially the 'if' logic becomes part of the 'where' part of the node selector.
The other alternative is to do one loop and create a temporary variable of nodes and count and loop that, which feels better than two separate loops, but not as ideal as being able to just have a single selector that takes the category filter into account.
Is it possible to do such a selector as part of the node-set gathering? Any pointers much appreciated.
Thanks folks!
It might be easier to use IPublishedContent for this which support strongly typed queries. Some more info here: http://our.umbraco.org/forum/developers/api-questions/46706-Razor-How-to-check-if-content-is-in-the-recycle-bin?p=0#comment167514
Jeroen
Thanks Jeroen.
I've not been able to combine this into a single statement unfortunately - it just won't seem to accept the 'or' (
||
) part of the expression. The closest I've got is this:However, I'm struggling to declare the correct type on the 'faqs' variable. I can't do
var faqs;
as "implicitly typed local variables must be initialized" and it errors if I doIPublishedContent faqs;
saying "Cannot implicitly convert system.linq.iqueryableAny ideas on this?
Thanks.
Actually, just got the last part -
IEnumerable<IPublishedContent> faqs;
sets the correct type.Your linq query will return an IEnumerable so try this:
That will return multiple IPublishedContent items you can loop through.
Jeroen
Wow I think you were a few seconds faster :-)
is working on a reply...
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.