我们如何检索 a 的第一项WhereSelectListIterator
?通常,我使用foreach
循环进行迭代。有没有办法调用等效的myResult[0]
or myResult.FirstOrDefault()
。两者都抛出错误。myResult.ToList()
也不行。我开始认为我们唯一能用 a 做的WhereSelectListIterator
就是迭代foreach
.
这是场景:我创建了一个带有 Shape 布局的 Orchard Query。形状模板包含以下代码:
@{
// content items is of type WhereSelectListIterator<T,T>
var contentItems = Model.ContentItems;
dynamic firstItem = null;
// {"Cannot apply indexing with [] to an expression of type 'object'"}
//// firstItem = contentItems[0];
// {"'object' does not contain a definition for 'ToList'"}
// var items = contentItems.ToList();
// get the title of the first content item
// this is what DOES work
foreach (var contentItem in contentItems)
{
firstItem = contentItem;
break;
}
}
<h2>@(firstItem != null ? firstItem.TitlePart.Title : "Got Nothing")</h2>
具体来说,contentItems
是类型
System.Linq.Enumerable.WhereSelectListIterator<
Orchard.Projections.Descriptors.Layout.LayoutComponentResult,
Orchard.ContentManagement.ContentItem>
如果您需要有关我为什么要检索第一个项目的更多详细信息,请告诉我。