2015-10-26 115 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(); 
    // ... 
}