使用C#在Umbraco节点列表上使用OrderBy

时间:2013-01-26 00:11:55

标签: c# umbraco

我在Umbraco节点的C#中有一个列表。我可以使用构建在节点类中的一些顶级属性(例如node.Name)成功地对这些进行排序,但是当我尝试使用GetProperty()函数获取一些动态属性时,我可以使用它。得到NullReferenceException。

代码:

nodes = nodes.OrderBy(n => n.GetProperty("fromPrice").ToString()).ToList();

n.GetProperty("fromPrice").ToString()替换n.Name有效,但我不想按此排序。

异常消息:

System.NullReferenceException: Object reference not set to an instance of an object.

不完全确定这意味着什么。任何帮助表示赞赏。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您可以从查询中的Where子句中受益,就像这样(从我的记忆中写入,未经过测试)

var nodes = nodes.Where(x => x.GetProperty("fromPrice") != null).OrderBy(n => n.GetProperty("fromPrice").Value);

首先应该过滤掉没有名为"fromPrice"的属性的节点,然后执行排序。使用Node对象时,我认为您不必致电ToString(),只需改为Value

让我知道这是如何工作的: - )