Best way to get collection of doctypes of certain type
I'm wondering what the best way to get all document types of a certain type. I'm running V4.7, using uSiteBuilder. Typically I would do this:
var newsArticles = uQuery.getNodesByType("NewsArticle");
This works fine when I only have 30-50 doctypes to get, but as soon as you start getting up over 100 things get pretty slow. It gets especially slow if you start making this call multiple times. For example, sometimes I need all the distinct years, so I would need to get all the doctypes again and select their years.
I've looked into caching the doctypes then checking the cache to see if they exist and pulling them from there.
I'm wondering if there is a better way to do this? What are the benefits of using Razor to do this sort of thing?
The uQuery method above internally uses an XPath expression and this is compiled after first use, so shouldn't be slow - constructing each of the Umbraco node objects for the result is the most expensive part part, so ideally you only want to construct those nodes you want to use.
How are you doing the second filter after the uQuery.GetNodesByType ? Are you using Linq with the newsArticles collection or re-requesting via GetNodesByType ?
typically what I would do is cast those nodes to a doctype so i can filter onone of that doctypes properties. I do this using uSitebuilder's ContentHelper and Linq:
var news = from news in newsarticles select ContentHelper.getByNodeId<documenttype.newsarticle>(news.id);
Once I have my nodes cast I would use Linq to filter them on a property in that doctype:
var orderedNews = from n in news where n.newsDate.Year == currentYear orderby news.newsdate descending select n;
In doing all this I can see that I am itereating through multiple collections of nodes/doctypes multiple times, which must be quite slow when each collection has a 100+ items. Also often times with things like news articles I need to display a menu of all the distinct years and display the top 10 or so articles for the current year, all on the same page. In this case I would write a method to iterate through the doctypes and return the distinct years, then I would need another method to iterate throguh the doctypes again and get the top 10 articles. So basicallly I end up itereating though these collections of 100+ doctypes over and over again on page load. This must be what's making it so slow right ???? But I am not sure of any other way to do this. Is there some way to cache the collection of nodes/doctypes so I don't have to reconstruct it multiple times on page load?
I'm not sure if I'm thinking about the correctly though...?
Best way to get collection of doctypes of certain type
I'm wondering what the best way to get all document types of a certain type. I'm running V4.7, using uSiteBuilder. Typically I would do this:
var newsArticles = uQuery.getNodesByType("NewsArticle");
This works fine when I only have 30-50 doctypes to get, but as soon as you start getting up over 100 things get pretty slow. It gets especially slow if you start making this call multiple times. For example, sometimes I need all the distinct years, so I would need to get all the doctypes again and select their years.
I've looked into caching the doctypes then checking the cache to see if they exist and pulling them from there.
I'm wondering if there is a better way to do this? What are the benefits of using Razor to do this sort of thing?
Thanks
Hi Tim,
The uQuery method above internally uses an XPath expression and this is compiled after first use, so shouldn't be slow - constructing each of the Umbraco node objects for the result is the most expensive part part, so ideally you only want to construct those nodes you want to use.
How are you doing the second filter after the uQuery.GetNodesByType ? Are you using Linq with the newsArticles collection or re-requesting via GetNodesByType ?
Cheers,
Hendy
typically what I would do is cast those nodes to a doctype so i can filter onone of that doctypes properties. I do this using uSitebuilder's ContentHelper and Linq:
var news = from news in newsarticles select ContentHelper.getByNodeId<documenttype.newsarticle>(news.id);
Once I have my nodes cast I would use Linq to filter them on a property in that doctype:
var orderedNews = from n in news where n.newsDate.Year == currentYear orderby news.newsdate descending select n;
In doing all this I can see that I am itereating through multiple collections of nodes/doctypes multiple times, which must be quite slow when each collection has a 100+ items. Also often times with things like news articles I need to display a menu of all the distinct years and display the top 10 or so articles for the current year, all on the same page. In this case I would write a method to iterate through the doctypes and return the distinct years, then I would need another method to iterate throguh the doctypes again and get the top 10 articles. So basicallly I end up itereating though these collections of 100+ doctypes over and over again on page load. This must be what's making it so slow right ???? But I am not sure of any other way to do this. Is there some way to cache the collection of nodes/doctypes so I don't have to reconstruct it multiple times on page load?
I'm not sure if I'm thinking about the correctly though...?
Thanks for the help Hendy
is working on a reply...