This has been doing my head in so I hope someone can help.
Here's the code in question - dead simple:
var rootNodeVal = int.Parse(Parameter.RootNode); var businessId = Model.Id; var rootNode = Library.NodeById(rootNodeVal); Dictionary<string,string> dic = new Dictionary<string,string>(); dic.Add("ModelId",Model.Id.ToString());
foreach(var node in rootNode.Descendants("Voucher").Where("Business == businessId")) { @node.Business.GetType() }
Now there's a couple of lines in there where I've been experimenting - namely the dictionary and the addition of the current page ID to it.
All it needs to do is select documents of type Voucher from anything below what I specify as the root node (rootNode).
In the Document type Voucher, there's a content picker to select a node, called Business.
Looking in umbraco.config I can see it's stored the value correctly ie.
<Business>1234</Business>
And if I iterate through the nodes without the where clause, it is finding these Voucher documents fine and dumping out the selected Business node ID. If I dump the type, it's a String.
So - why is it when I use the where clause ie. Where("Business == businessId"), or Where("Business == ModelId",dic), it is not finding the appropriate nodes when I know they're there.
I've tried everything - casting to int, strongly typing the variables etc - nothing works. I'm proper stuck so any help much appreciated.
Some more info - here is the result of the iteration with the Where clause set to "Business != null" :
This document is node id 4637
Voucher ID 6680 property Business is set to , of type System.String
Voucher ID 6683 property Business is set to 4637, of type System.String
So there filter is obviously working, but not if I directly compare to the document (Model) node id, which is 4637. As you can see, document 6683 has the business set to node id 4637 - so with the filter I mentioned above applied it should list it - but it doesn't.
Turned out it was my declaration of the dictionary - string,string didn't work - it was string,object that sorted it. Setting the id in the dict via ToString() did the trick. Still not quite sure why string,string didn't work though so if anyone could make me any wiser I'd be grateful.
Cheers Jeroen - that's a really interesting post and might well prove useful later. I'm wondering if the same kind of performance impact is felt if you called a delegate to filter the nodeset instead? I use that alot in parts, especially for filtering using more complex checks. Might have to do some tests :/
Need help with weird Where clause selection
Hi all,
This has been doing my head in so I hope someone can help.
Here's the code in question - dead simple:
Now there's a couple of lines in there where I've been experimenting - namely the dictionary and the addition of the current page ID to it.
All it needs to do is select documents of type Voucher from anything below what I specify as the root node (rootNode).
In the Document type Voucher, there's a content picker to select a node, called Business.
Looking in umbraco.config I can see it's stored the value correctly ie.
And if I iterate through the nodes without the where clause, it is finding these Voucher documents fine and dumping out the selected Business node ID. If I dump the type, it's a String.
So - why is it when I use the where clause ie. Where("Business == businessId"), or Where("Business == ModelId",dic), it is not finding the appropriate nodes when I know they're there.
I've tried everything - casting to int, strongly typing the variables etc - nothing works. I'm proper stuck so any help much appreciated.
Cheers!
Tony
It's 4.7.1.1 btw. :)
Some more info - here is the result of the iteration with the Where clause set to "Business != null" :
This document is node id 4637
Voucher ID 6680 property Business is set to , of type System.String
Voucher ID 6683 property Business is set to 4637, of type System.String
So there filter is obviously working, but not if I directly compare to the document (Model) node id, which is 4637. As you can see, document 6683 has the business set to node id 4637 - so with the filter I mentioned above applied it should list it - but it doesn't.
Gah! Typical - perserverance always pays off.
Turned out it was my declaration of the dictionary - string,string didn't work - it was string,object that sorted it. Setting the id in the dict via ToString() did the trick. Still not quite sure why string,string didn't work though so if anyone could make me any wiser I'd be grateful.
Glad you got it fixed, but be careful about using .where too much. More info in this topic: http://our.umbraco.org/forum/developers/razor/28479-Razor-menu-performance-(v4)
Jeroen
Cheers Jeroen - that's a really interesting post and might well prove useful later. I'm wondering if the same kind of performance impact is felt if you called a delegate to filter the nodeset instead? I use that alot in parts, especially for filtering using more complex checks. Might have to do some tests :/
is working on a reply...