In short, if you need to use .Contains, you can’t. Switch statements are equals expressions, they cannot mimic .Contains
The only possible way to approach this using a switch statement is to make sure the input to the switch only contains the data you want to check against. That means the full value of node.Data.Property_Taxonomy can only contain 0/2/2437/2446 or the other variations you have within it. Or you must utilize node.Data.Property_Taxonomy to set a new variable to only the values the switch statement wants to receive.
I did think this might be the issue, been reading a few google posts - people reverting back to if else
I think I understand. Sounds like this could work?
The taxonomy in discussion only has values of this sort, the format 0/2/2437/2446 will always be like this with a variation for each item.
Could you show example of how this could potentially work?
Or this will not work, best stick with if else?
Updated query above in #1 if makes any difference.
Also wondering below, when I was using the if else statements I tried to add a variable to reduce DRY, again, this didn’t work. What is the correct way to add/use a variable in this way?
var x = "node.Data.Property_Taxonomy.Contains";
if (@x("0/2/2437/2446"))
var x = node.Data.Property_Taxonomy;
if (x.Contains("0/2/2437/2446"))
You must have more data than just “0/2/2437/2446” in that property because otherwise the switch statement would work. Can you output the entire property value or set a breakpoint on it in a debugger to see what the true value is?
Correct the taxonomy is called Icons with a number of children, the icons:
Taxonomy Node Name
Node Key : 0/2/2437
Node Name : Icons
Taxonomy Node Name (child)
Node Key : 0/2/2437/2448
Node Name : Heart
StructuredContent (for reference)
-Icons
-- Glass
-- Heart
-- Music
-- Star
Can you output the entire property value or set a breakpoint on it in a debugger to see what the true value is?
A little tricky as I’m working in a CMS, the razor views are included files not sure how to get the values except what is show above, which is taken from the taxonomy GUI
Regarding the variable…
I did try this in two slightly different instances both producing errors:
Error: Cannot invoke a non-delegate type
and
error CS0118: ‘x’ is a ‘variable’ but is used like a ‘method’