Get list of Descendants with document type and property value
I am trying to get a list of Descendants from the root node with a certain type and property value. This is from a PartialViewMacro page.
On document type myType, there is a textstring property myProperty. To extract all nodes with the relevant document type, and a value of "blue" stored in myProperty, I am using the following code:
//get all nodes with correct document type
var list = CurrentPage.AncestorOrSelf(1).Descendants("myType");
//filter on myProperty
var filteredList = list.Where("myProperty == @0", "blue");
However, filteredList is always empty, even though list contains multiple nodes with the correct document type and a property value of "blue".
Are there any better approaches to achieve the correct results that I could try?
I have just tried to use the code that you have posted.
And it work fine for me. I have added a property with the alias of newprop, and in my example it uses the textsting data type.
Here is how my code looks like, it just pull out the name of the item.
@inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var list = CurrentPage.AncestorOrSelf(1).Descendants("umbNewsItem"); var filteredList = list.Where("newprop == @0", "blue");
I have no idea what changes I have made, but upon comparing my .cshtml file to yours, and deleting/re-creating the macro, the code is working. All I can assume is that there was some kind of typo that I originally missed - just shows the importance of checking for the simplest mistakes!
Get list of Descendants with document type and property value
I am trying to get a list of Descendants from the root node with a certain type and property value. This is from a PartialViewMacro page.
On document type
myType
, there is a textstring propertymyProperty
. To extract all nodes with the relevant document type, and a value of "blue" stored inmyProperty
, I am using the following code:However,
filteredList
is always empty, even thoughlist
contains multiple nodes with the correct document type and a property value of "blue".Are there any better approaches to achieve the correct results that I could try?
Hi Phil,
I have just tried to use the code that you have posted.
And it work fine for me. I have added a property with the alias of newprop, and in my example it uses the textsting data type.
Here is how my code looks like, it just pull out the name of the item.
Hope this helps,
/Dennis
@Dennis,
I have no idea what changes I have made, but upon comparing my .cshtml file to yours, and deleting/re-creating the macro, the code is working. All I can assume is that there was some kind of typo that I originally missed - just shows the importance of checking for the simplest mistakes!
Your help is very much appreciated.
Hi Phil,
You are welcome, and great it works now,
/Dennis
is working on a reply...