Copied to clipboard

Flag this post as spam?

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


  • fed 199 posts 70 karma points
    Jul 22, 2010 @ 00:39
    fed
    0

    Simple Linq2umbraco question

    I have a couple of questions that I can't figure out on my own :/

    I have the DataContext setup correctly and classes created for my documenttypes.

    1. What I want to do now is list all pages below the current page, regardless their documenttype and regardless of what page I am on, using the DataContext. (In XSLT I would just loop through $currentPage/node)

    2. Find out the current startpage (for a multilingual site)
    (In XSLT: $currentPage/ancestor-or-self::node [@level=1] or something similar)

    Maybe I have missed something obvious or have misunderstood the concept completely.

    I know I can use the NodeFactory etc but is it possible to accomplish this with the DataContext?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 22, 2010 @ 00:58
    Aaron Powell
    0

    1. LINQ to Umbraco wont give you the 'current page', it doesn't have that concept, especially since each 'current page' may be a different type then how is it meant to work out which type you actually require?

    If you have an instance you can get all it's children using the .Children property.

    2. DocTypeBase has a method on it called AncestorOrDefault which allows you to query back up from the instance. This requires a type argument to be passed to it to define the type of ancestor you are trying to find (you can also provide a boolean returning function if you have many parents of the same type).

     

    Something people need to understand that LINQ to Umbraco is statically typed, where as XPath is dynamically typed. Something that is possible in XPath is not necessarily possible in LINQ to Umbraco.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 22, 2010 @ 01:55
    Stefan Kip
    0

    1: <YourCollectionOfTheCurrentKnownType>.FirstOrDefault(x => x.Id == Node.GetCurrent().Id)

    Should work right? Only downside is that you'll have to know the type of the current node, but if you don't than you shouldn't use Linq2Umbraco anyway for the situation. Instead Node.GetCurrent()

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 22, 2010 @ 02:02
    Aaron Powell
    0

    Yes that will work but it's not optimal as you're really parsing the XML twice, once for LINQ to Umbraco and once for Node.GetCurrent().

    To get the current ID you're best using the UmbracoContext:

    int pageId = UmbracoContext.Current.PageId.Value;
  • smercer 39 posts 67 karma points
    Jul 22, 2010 @ 03:24
    smercer
    0

    I think Linq2Umbraco would really benefit from a generated class factory. I'll probably write one manually when I get deeper into my next umbraco project, but it would be awesome if the code generator spit one out for these situations where you just want a strongly typed object returned for your current node.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 22, 2010 @ 06:51
    Aaron Powell
    0

    There already is a package which will generate the classes for you: http://our.umbraco.org/projects/developer-tools/autoexport2dotnet

    But I don't know how you'd expect the node to know the type required to do on the fly generation...

  • smercer 39 posts 67 karma points
    Sep 17, 2010 @ 20:10
    smercer
    0

    what I  meant by class factory, is a factory method, where you could pass it a node ID and it would return a strongly typed Linq2Umbraco POCO cast to an interface (IDocTypeBase?). It could use reflection or just a big switch block that could be generated at the same time one would generate their Linq2Umbraco classes. Just a thought. Manually adding to that switch block every time I add a doc type to umbraco would be nice to avoid.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 18, 2010 @ 01:19
    Aaron Powell
    0

    What you're wanting would probably result in a load of overhead and it's not really what it was LINQ to Umbraco was designed for: http://www.aaron-powell.com/understanding-linq-to-umbraco

Please Sign in or register to post replies

Write your reply to:

Draft