2015-10-26 112 views
-1

我有這個foreach函數,但我需要添加一個where子句。加入where子句

我已經加入一把umbraco一個CheckBoxList的所謂的「秀」 值,如果這是

"EN" 
"SP" 
"US" 
... 

讓我們說,我已經檢查EN和SP。

我只希望幻燈片可見,如果幻燈片是可見的像現在一樣,並且字段show是「EN」,則選中併爲true。我怎樣才能在我的代碼中添加這個?

@foreach (var Slider in Umbraco.ContentSingleAtXPath("//HomePage/SliderArea").Children.Where("Visible").OrderBy("CreateDate 
desc").Take(4)) 
+0

哪個版本一把umbraco ? – wingyip

回答

0

你的代碼是使用動態,因此你只能使用像.Where("Visible")僞LINQ的擴展。如果使用Typed對象,則會發現操作項目列表會更容易。

更改此:

// Returns IPublishedContent as dynamic 
Umbraco.ContentSingleAtXPath("//HomePage/SliderArea") 

這樣:

// Returns fully typed IPublishedContent 
Umbraco.TypedContentSingleAtXPath("//HomePage/SliderArea") 

然後你就可以使用Linq的全部力量來做到這一點:

var area = Umbraco.TypedContentSingleAtXPath("//HomePage/SliderArea"); 

// returns a filtered IEnumerable<IPublishedContent> 
var sliders = area.Children.Where(c => c.IsVisible() && c.GetPropertyValue<string>("show") == "EN"); 

@foreach (IPublishedContent slider in sliders.OrderByDescending(c => c.CreateDate).Take(4)) 
{ 
    // You can get the dynamic equivalent of the IPublishedContent like this if you wish: 
    dynamic dSlider = slider.AsDynamic(); 
    // ... 
}