Umbraco 7.12:如果 X 匹配当前页面 ID,或当前页面的任何后代 - 这可能吗?

Umbraco 7.12: If X Matches Current Page ID, OR Any of Current Page's Descendants - is it possible?

得到我放在一起的这个片段:

@foreach(var item in selection){
    if(item.HasValue("location") && @Model.Content.Id == @item.GetPropertyValue<int>("location")){
var singleLink = item.GetPropertyValue<Link>("links");
    if(singleLink != null)
    {
        if(item.HasValue("wideImage"))
            {  
            IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
               <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target"><img class="tag-headadvert img-responsive" style="display: inline;" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a></div> 
            }
    }
}

在第二行你可以看到我将当前页面 ID 与一个变量进行了比较,如果该变量与当前页面 ID 或当前页面 ID 的任何后代相匹配,有没有办法做?

谢谢

您需要检索后代 ID 列表 - 最简单的方法之一是 select 来自 Model.Content.DescendantsOrSelf() 的 ID - 例如:

var descendantIds = Model.Content.DescendantsOrSelf().Select(c => c.Id);

然后你可以在第二行使用descendantIds.Contains(@item.GetPropertyValue<int>("location"))

注意:如果您有大量当前节点的后代,您可能会发现您需要以某种方式优化查询,可能使用缓存或想出一种完全不同的方式来获得您想要的结果。