Get a specific page count under a parent page without returning all the page instances

Vote:
 

I'd like to get the count for a specific page type under a specific parent page. I tried using the below. But I think it's resource heavy when comes to only getting the count. (Assume we have thousands of pages and I need to get just the count without returning all the pages).

IContentLoader _loader = ServiceLocator.Current.GetInstance<IContentLoader>();
IEnumerable<PageType> pages = _loader.GetChildren<PageType>(parentPageRef);
int count = pages.Count();

Is there a way we could only return page count with good performance?

Note: Episerver Find is not preferred.

#225150
Edited, Jul 07, 2020 6:40
Vote:
 

Hi,

Episerver Find is the best way to do this but since you are not want to use that then you can use cast method and it will eliminate the items-

Please see this -

IContentLoader _loader = ServiceLocator.Current.GetInstance<IContentLoader>();
IEnumerable<BlogCommentPage> pages = _loader.GetChildren<PageType>(parentPageRef).Cast<BlogCommentPage>();;
int count = pages.Count();

or you can refer below article but that is not recommended way to do this -

http://joelabrahamsson.com/get-child-pages-filtered-by-page-type/

#225151
Edited, Jul 07, 2020 7:47
- Jul 07, 2020 7:57
Hi. Thanks. I've updated the forum post. Sorry. It's not about casting.
Ravindra S. Rathore - Jul 07, 2020 8:05
Refer to the article that I referred in my comment
- Jul 08, 2020 3:27
That article explains to do it from EPiServer.DataFactory. Which is legacy.
Vote:
 

I believe you can try to use to find pages with criteria (please google it, I cant paste a link). I think there should be a way of getting pages by parent ID.

If it will not work, I think only caching is something that can improve the performance for your query.

#225153
Jul 07, 2020 8:03
Vote:
 

I don't know your particular use case, so this may or may not work for you.

A totally different approach would be to have a hidden field on the parent page itself. A scheduled job can run daily/hourly and can populate this field for you. That way you are forcing the CMS do the work for you than putting the onus on the front-end users. Again - not sure if this is the right approach without knowing your particular use case.

#225163
Jul 07, 2020 12:07
Vote:
 

Good idea, but it also can be improved by using published event instead of job. 

#225164
Jul 07, 2020 12:31
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.