Hi, I'm trying to simply list (for navigation) a unique/distinct property named "category":
<ul>
<li>Category1</li>
<li>Category2</li>
</ul>
Many of the nodes I'm looping through will contain the same "category" property, but I only want it to appear once in the list, eg NOT what I'm getting at the moment, which is…
var selection = CurrentPage.Children.Where("Visible");
foreach( var page in selection.Distinct("category") ) {
<li><a href="@page.category">@page.category</a></li>
}
I know this won't work(!), but hopefully it illustrates what I'm trying to do.
There's a variable number of pages under this node. Currently 6 categories, but more than one of the pages will have the same category. I use a foreach loop to display the pages, and another foreach loop to create the 'navigation': it's this which needs to show each of the categories in use by the current selection of pages retrieved… but only once per category: currently I'm getting every instance of the category property. Eg:
Thanks for your help, Nicholas. Unfortunately I don't see a way of getting the value of the property rather than a number. At the moment I'm getting 133, 135 instead of Eat, Shop…
List distinct property in foreach loop
Hi, I'm trying to simply list (for navigation) a unique/distinct property named "category":
Many of the nodes I'm looping through will contain the same "category" property, but I only want it to appear once in the list, eg NOT what I'm getting at the moment, which is…
I assumed it would be something like this:
I know this won't work(!), but hopefully it illustrates what I'm trying to do.
Thanks
Robin
How many pages do you have? Is it duplicating the same ones or is there actually that amount just wrong text?
There's a variable number of pages under this node. Currently 6 categories, but more than one of the pages will have the same category. I use a foreach loop to display the pages, and another foreach loop to create the 'navigation': it's this which needs to show each of the categories in use by the current selection of pages retrieved… but only once per category: currently I'm getting every instance of the category property. Eg:
page1 category="category1" page2 category="category1" page3 category="category4"
So I show all the pages, 1 to 3, but only "category1" and "category4" in the nav, not "category1, category1, category4"…
I would not recommend dynamic syntax. Instead, try this:
Thanks, Nicholas, but I never use Model, only ever CurrentPage.etc… so I have no idea how to implement this :(
I currently have something like:
… and I'm not sure how I'd use the non-dynamic syntax to operate on children of the current page.
OK, so…
If I put @page inside foreach loop it prints out 'UMBRACO.WEB.PUBLISHEDCACHE…'
I'd have thought this would work…
… but it gives a compilation error
What's the compilation error? Try assigning it to a variable first. For example:
By the way,
@page.Url
will give you the page's URL.Thanks for your help, Nicholas. Unfortunately I don't see a way of getting the value of the property rather than a number. At the moment I'm getting 133, 135 instead of Eat, Shop…
Looks like you are getting the prevalues (basically, a number representing the value). Here's how you get the text version of a prevalue:
is working on a reply...