Listing nodes where checkbox property contains specific value
I am trying to list all nodes where a checkbox property (Season) contains a specific value. So far I have this, which is working for the most part:
myNodes.Where("season.Contains(@0)", pfSeason);
However, the values of Season can be:
Early Spring
Spring
Late Spring
Summer
Late Summer
Fall
Winter
When I attempt to list all nodes that have Spring selected, it returns nodes that have Early Spring and Late Spring selected as well because Spring is contained within those values. How can I pull only nodes that have Spring selected?
From the trace: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Also, maybe worth mentioning this is built with the old Razor macro rather than a Partial View.
var pfSeason = Request["pf-season"];
var pagesToList = CurrentModel.Descendants("Product").Items;
pagesToList = pagesToList.Where(x => x.HasProperty("season") && x.HasValue("season") && x.GetPropertyValue("season").Split(',').Contains(pfSeason)).ToList();
you need to check if the property has value first. (if all the pages have got property season then you can remove the first criteria (HasProperty). I would leave it, just in case :)
Listing nodes where checkbox property contains specific value
I am trying to list all nodes where a checkbox property (Season) contains a specific value. So far I have this, which is working for the most part:
However, the values of Season can be:
When I attempt to list all nodes that have Spring selected, it returns nodes that have Early Spring and Late Spring selected as well because Spring is contained within those values. How can I pull only nodes that have Spring selected?
Hi Derrik
can you try this?
Ali, it returned an error:
No applicable method 'Split' exists in type 'String'
can you tell me what version of Umbraco you are using? are you also using razor?
Umbraco v6.1.6, and yes I'm using Razor.
Here is the rest of the code, if that helps:
Hi,
I might be off track here but aren't you just looking for
Jeavon
Jeavon, I think its checkbox so I am assuming that the selection can be multiple so it should generate CSV.
how about this one!
Hi Jeavon,
The property is a checkbox, which can contains multiple values so it may not always equal pfSeason.
Ok, I guess you mean checkbox list then. I think Ali's above suggestion should work?
Hi Derrik,
did the above code worked?
Cheers
Ali
From the trace: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Also, maybe worth mentioning this is built with the old Razor macro rather than a Partial View.
is your
Model
dynamic? if so then can you useCurrentPage
instead ofModel
?As in
CurrentPage.Descendants("Product")
? I tried and got The name 'CurrentPage' does not exist in the current contextok then here is what you need
as you might have noticed I've changed the
Model
toCurrentModel
because Model is dynamic whereas CurrentModel is strongly typed.I've also added
.Items
to get the items for the collection.I believe the above code should work
Cheers
Ali
Success! Thank you very much!
you are welcome :)
Sorry, one more quick question regarding this. I'm reusing this logic for a few other properties and I ran into an issue.
On some of the nodes the property (Habit) does not have a value and I get Object reference not set to an instance of an object.
here is your answer:
you need to check if the property has value first. (if all the pages have got property season then you can remove the first criteria (HasProperty). I would leave it, just in case :)
Ah, that makes sense. Thanks again!
is working on a reply...