2016-02-29 83 views
0

在Umbraco 7解決方案中,我在所有頁面上都有一個Tags內容選擇器。頁面可以用這個,在每個頁面上設置標籤。查找標籤頁

然後,我想要在包裝網站中獲取包含標籤111(id,not name)的alle頁面。

我曾嘗試用:

var ids = Model.MacroParameters["tags"]; //the tags to show 
CurrentPage.AncestorOrSelf(1).Descendants().Where(x => ids.Contains(x.tags.ToString())); 

但是,這給我的錯誤:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type 

請告訴我正確的方法是什麼?

回答

1

解決了;

Umbraco.Content(rootId).Descendants().Where("tags.Contains(@0)", ids); 
1

根據您喜歡動態還是強類型視圖模型,您有幾個選項。

強類型的API

Umbraco.TypedContentAtRoot().Descendants().Where(x => x.tags.Contains(ids)); 

動態API

Umbraco.ContentAtRoot().Descendants().Where("tags.Contains(@0)", ids); 

請注意:包含聲明可能會給你不一致的結果,因爲標籤屬性似乎是返回一個逗號分隔的列表。在這種情況下,您可以嘗試拆分字符串或安裝Core Property Value Converters package並獲取標籤IEnumerable<IPublishedContent>

0

總是儘量避免使用Descendants,特別是在根節點上。 要獲取標籤的屬性:

ApplicationContext.Current.Services.TagService.GetTagsForProperty(Model.Content.Id, "propertyname") 

要找到一個特定的標籤內容:

ApplicationContext.Current.Services.TagService.GetTaggedContentByTag("tag")