Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ayo Adesina 445 posts 1059 karma points
    Nov 15, 2011 @ 00:44
    Ayo Adesina
    0

    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......

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 15, 2011 @ 01:51
    Tom Fulton
    0

    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

  • Ayo Adesina 445 posts 1059 karma points
    Nov 15, 2011 @ 20:39
    Ayo Adesina
    0

    Thanks tom

  • Ayo Adesina 445 posts 1059 karma points
    Nov 15, 2011 @ 21:06
    Ayo Adesina
    0

    sermosNode.Children.Cast().OrderBy(n => n.CreateDate).FirstOrDefault();  << it didn't work you need to do this..it needs a cast

Please Sign in or register to post replies

Write your reply to:

Draft