获取数组中的最后 child

Get last child in array

我最近开始使用 Umbraco,我正在尝试从数组中获取最后一个 child。这是我到目前为止所做的:

@inherits Umbraco.Web.Macros.PartialViewMacroPage

@{var page = Umbraco.Content(1127).Children[Children.Length - 1]}

<h5><a href='@page.Url'>@page.NewsTitle</a></h5>
<p>@page.NewsIntro</p>
<p class='read-more'><a href='@page.Url'>Read more...</a></p>

编辑:

解决方法如下:

@inherits Umbraco.Web.Macros.PartialViewMacroPage

@{
    var length = Umbraco.Content(1127).Children.Count();
    var page = Umbraco.Content(1127).Children[length - 1];
}

<h5><a href='@page.Url'>@page.NewsTitle</a></h5>
<p>@page.NewsIntro</p>
<p class='read-more'><a href='@page.Url'>Read more...</a></p>

尝试:@{var page = Umbraco.Content(1127).Children[Children.Length - 1]}

请注意 Length 中的大写 L。 c# 区分大小写,不存在名为 length.

的数组 属性

如果您能够在我看到的 C# 代码中使用 Linq,它有一个您可以使用的 "Last()" 扩展方法。

像这样:

        @using System.Linq
        @{var page = Umbraco.Content(1127).Children.Last()}

Umbraco 有一堆 "isHelpers" 包括 IsLast()。这是一个使用 isLast.

的例子
foreach(var page in Umbraco.Content(1127).Children)
{
    if(page.IsLast())
    {
        <h5><a href='@page.Url'>@page.NewsTitle</a></h5>
        <p>@page.NewsIntro</p>
        <p class='read-more'><a href='@page.Url'>Read more...</a></p>
    }
}

这是 Umbraco 公开的其他一些 isHelpers。

IsHelper Methods .IsFirst([string valueIfTrue][,string valueIfFalse]) Test if current node is the first item in the collection

.IsNotFirst([string valueIfTrue][,string valueIfFalse]) Test if current node is not first item in the collection

.IsLast([string valueIfTrue][,string valueIfFalse]) Test if current node is the last item in the collection

.IsNotLast([string valueIfTrue][,string valueIfFalse]) Test if current node is not the last item in the collection

.IsPosition(int index[,string valueIfTrue][,string valueIfFalse]) Test if current node is at the specified index in the collection

.IsNotPosition(int index[,string valueIfTrue][,string valueIfFalse]) Test if current node is not at the specified index in the collection

.IsModZero([string valueIfTrue][,string valueIfFalse]) Test if current node position evenly dividable (modulus) by a given number

.IsNotModZero([string valueIfTrue][,string valueIfFalse]) Test if current node position is not evenly dividable (modulus) by a given number

.IsEven([string valueIfTrue][,string valueIfFalse]) Test if current node position is even

.IsOdd([string valueIfTrue][,string valueIfFalse]) Test if current node position is odd

.IsEqual(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is equivalent (by Id) to another node

.IsDescendant(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is a descendant of another node

.IsDescendantOrSelf(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is a descendant of another node or is the node

.IsAncestor(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is an ancestor of another node

.IsAncestorOrSelf(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is an ancestor of another node or is the node