Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ianhoughton 281 posts 605 karma points c-trib
    Oct 30, 2012 @ 17:51
    ianhoughton
    0

    Trying to OrderBy Date on list returned in C#

    I'm trying to pull the latest 5 articles from the news section and then sort them by the article date.

    I've managed to get the 5 articles and display them ok, but the OrderBy is not working as expected. I need to sort them by a property on the node called "NewsDate" which is a date picker.

    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;
    }
  • Sean Mooney 131 posts 158 karma points c-trib
    Oct 30, 2012 @ 18:38
    Sean Mooney
    0

    GetProperty will return the value as a string, so you will need to change the type to a date then it should work.

    something like this:

    var nodesOutput = nodes.OrderBy(n => (Date)n.GetProperty("NewsDate").Value);
Please Sign in or register to post replies

Write your reply to:

Draft