如何查找Dynamic NodeList中的项目数?

时间:2012-06-05 08:13:11

标签: c# razor umbraco

DynamicNodeList是否有任何属性或函数返回列表项的数量。

这是我的代码:

var root = Model.NodeById(id);
var nodes = root.Descendants("ChartItem");

if (nodes.GetLength() > 0)
{
   s = s + "<ul>";
}

GetLength 不是有效的功能。我该怎么办?

3 个答案:

答案 0 :(得分:4)

有一个名为Count()的IEnumerable类型的内置扩展方法,就是这样,它计算项目:)

见下面的代码:

var root = Model.NodeById(id);
var nodes = root.Descendants("ChartItem");

if (nodes.Count() > 0)
{
    s = s + "<ul>";
}

答案 1 :(得分:1)

试试这段代码:

var root = Model.NodeById(id);
var nodes = root.Descendants("ChartItem");

int nodesCount = 0;
foreach (var node in nodes)
{
    nodesCount += 1;
}

if (nodesCount > 0)
{
    s = s + "<ul>";
}

答案 2 :(得分:1)

看这里

http://umbraco.com/follow-us/blog-archive/2011/12/22/umbraco-5-rc1-is-out-today.aspx

我会尝试(不确定是否需要空检查,或者Descendants()是否至少返回一个空列表)

nodes != null && nodes.Count() >0

nodes != null && nodes.Any()