Copied to clipboard

Flag this post as spam?

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


  • greengiant83 88 posts 109 karma points
    Feb 24, 2011 @ 14:56
    greengiant83
    0

    Convert INode to Linq to Umbraco Entity

    I would like to be able to get a Linq to Umbraco entity (the strongly typed models of the actual documents) that represents the requested page.  Similar to umbraco.NodeFactory.Node.GetCurrent() but with the strongly typed entity returned instead.

    Is there a way to instantiate an entity from a node? So far the only way I see to do it is to do a lookup on the ID.  MyDataContext.MyObjects.Single(i => i.Id == currentNodeId) 

    Also it would be nice to be able to make it generic such that I could just specify a type and convert the current node to it.  Something like

     

            public static T Active<T>()
            {
                return DocTypeBase.GetFromNode<T>(umbraco.NodeFactory.Node.GetCurrent());
            }

     

    Does that make any sense?

  • greengiant83 88 posts 109 karma points
    Feb 24, 2011 @ 15:13
    greengiant83
    0

    I think I mostly found my answer, but I wonder if there isnt a more effecient way.  This is an additional method on the data context added via a seperate partial class 

     

            public  T Active() where T: DocTypeBase, new()
            {
                return DataProvider.Load(umbraco.NodeFactory.Node.getCurrentNodeId());
            }

     

    Can you anyone think of a more effecient (in terms of run time) to do this?

     

    EDIT:

    Having played a bit this, it should be noted that the DataProvider.Load method will populate the object with only the properties from the object.  Any relationships it might have with sub types will not be populated

Please Sign in or register to post replies

Write your reply to:

Draft