How do I get a child node in c#, is it possible to run somehting like a linq query over the children to get a specific one?
I have tried to do somehting like this...
Node myNode= new umbraco.presentation.nodeFactory.Node(1083);
myNode.Children.Where(p => p.property...bla bla bla
but there are no options for this... The child nodes inside myNode have a date property I want to get the date that is the most current..
Side note: I know I can do this in XSLT and normally I would but I started using the umbraco api and I prefer writing c# rather than XSLT, I guess XSLT will have a lot better performance, but does anyone know if is pratical to use loads of user controls rather than xslt files is performance going to be an issue if all macros are done this way?
You should also check out uQuery which has some helpful methods and extension methods to help you select nodes, and you can use the extension method to select your properties with their type, which might help you when selecting a date, ie: myNode.Children.Where(p => p.GetProperty<int>("myProperty") = 1)
I think the performance is about the same because XSLT and NodeFactory are both querying the cached XML. What gets slower is if you use the Document API instead of the NodeFactory. Also I'm not an expert but maybe it's slower to iterate over a large array of children using .Where() etc in .NET. I have seen some spots in the core where they store the Children in a variable first, ie var childDocs = myNode.Children; then iterate the variable. But not sure on the specifics :)
Get Specific child node using c# - usercontrol
How do I get a child node in c#, is it possible to run somehting like a linq query over the children to get a specific one?
I have tried to do somehting like this...
Node myNode= new umbraco.presentation.nodeFactory.Node(1083);
myNode.Children.Where(p => p.property...bla bla bla
but there are no options for this... The child nodes inside myNode have a date property I want to get the date that is the most current..
Side note: I know I can do this in XSLT and normally I would but I started using the umbraco api and I prefer writing c# rather than XSLT, I guess XSLT will have a lot better performance, but does anyone know if is pratical to use loads of user controls rather than xslt files is performance going to be an issue if all macros are done this way?
any ideas......
Hi,
You should be able to do a .Where like you describe, it should be something like:
myNode.Children.Where(p => p.getProperty("myProperty").Value == "myvalue")
You should also check out uQuery which has some helpful methods and extension methods to help you select nodes, and you can use the extension method to select your properties with their type, which might help you when selecting a date, ie: myNode.Children.Where(p => p.GetProperty<int>("myProperty") = 1)
I think the performance is about the same because XSLT and NodeFactory are both querying the cached XML. What gets slower is if you use the Document API instead of the NodeFactory. Also I'm not an expert but maybe it's slower to iterate over a large array of children using .Where() etc in .NET. I have seen some spots in the core where they store the Children in a variable first, ie var childDocs = myNode.Children; then iterate the variable. But not sure on the specifics :)
Hope this helps,
Tom
Thanks tom
sermosNode.Children.Cast().OrderBy(n => n.CreateDate).FirstOrDefault(); << it didn't work you need to do this..it needs a cast
is working on a reply...