我正在尝试从新闻部分提取最新的 5 篇文章,然后按文章日期对它们进行排序。
我设法得到了 5 篇文章并正常显示,但 OrderBy 没有按预期工作。我需要通过节点上名为“NewsDate”的属性对它们进行排序,该属性是一个日期选择器。
protected void Page_Load(object sender, EventArgs e)
{
var currentNode = Node.GetCurrent();
var nodeTypeAlias = currentNode.NodeTypeAlias;
if (nodeTypeAlias == "NewsMasterPage")
{
var nodes = FindChildren(currentNode, t => t.NodeTypeAlias.Equals("NewsArticle"));
var nodesOutput = nodes.OrderBy(n => n.GetProperty("NewsDate"));
newsRepeater.DataSource = nodesOutput;
newsRepeater.DataBind();
}
}
private static List<Node> FindChildren(Node currentNode, Func<Node, bool> predicate)
{
List<Node> result = new List<Node>();
var nodes = currentNode
.Children
.OfType<Node>()
.Where(predicate).Take(4);
if (nodes.Count() != 0)
result.AddRange(nodes);
foreach (var child in currentNode.Children.OfType<Node>())
{
nodes = FindChildren(child, predicate);
if (nodes.Count() != 0)
result.AddRange(nodes);
}
return result;
}