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?
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.
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()
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.
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.
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?
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.
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()
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:
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.
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...
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.
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
is working on a reply...