levipadre
levipadre

Reputation: 619

Get children's children count in Umbraco

How can I count the children's children nodes? I have this code so far:

@foreach (var newsYear in CurrentPage.Children.Where("Visible"))
{
    @newsYear.Name (@newsYear.Children.Count()) @*Number of children's children*@
    @foreach (var newsMonth in newsYear.Children.Where("Visible"))
    {
        @newsMonth.Name (@newsMonth.Children.Count())
    }
}

Upvotes: 1

Views: 2488

Answers (2)

Jerode
Jerode

Reputation: 500

Use the SelectMany() to do this.

var allItems = CurrentPage.Children.Where("Visible").ToList();
foreach (var newsYear in allItems)
{
    var yearTotal = allItems.SelectMany(c => c.Children).Count();
    foreach (var newsMonth in newsYear.Children)
    {
        var monthTotal = newsMonth.Children.Count();
    }
}

Upvotes: 0

Craig Mayers
Craig Mayers

Reputation: 185

From looking at your code, it looks like you are trying to display the total number of all news articles for the current year?

 @newsYear.Name (@newsYear.Children.Count())

However, this would return the count for all the 'month' nodes, assuming you are following the following structure:

Year > Month OR Year > Month > Day

If you want the Total number of articles for the current month (in loop), then try the following:

@newsYear.Children.Where(x => x.DocumentTypeAlias.Equals("YourNewsDocTypeAliasHere")).Count();

Upvotes: 1

Related Questions