Copied to clipboard

Flag this post as spam?

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


  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 14:05
    dominik
    0

    Order by in foreach

    Hello,

    I am trying to order my news items inside a foreach loop by CreateDate but I cant get it working

     foreach (dynamic newsItem in item.NewsItem.OrderBy("CreateDate desc")

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 30, 2012 @ 14:21
    Fuji Kusaka
    0

    Hi Dominik

    Try something like

    var sortedItems = newsItems.Children.Where("Visible").OrderBy("createDate descending");
    @foreach (dynamic page in sortedItems){
    //
    }

     

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 14:28
    dominik
    0

    HI Fuj,

    Instead of my foreach loop? Can you also please explain why my statement does not work?

    Thanks

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 30, 2012 @ 17:14
    Fuji Kusaka
    0

    Hi Dominik,

    Not sure why your foreach loop is not working, could you post your whole code?

    //fuji

  • Sören Deger 733 posts 2844 karma points c-trib
    Aug 30, 2012 @ 17:56
    Sören Deger
    0

    Hi Dominik,

    Try this instead of your foreach loop

    @foreach (dynamic newsItem in item.NewsItems.Where("Visible").OrderBy("CreateDate desc")) {
    //
    }

    or this

    @foreach (dynamic newsItem in item.Children.Where("Visible").OrderBy("CreateDate desc")) {
    //
    }

    Perhaps you have forgot the 's' of the end of 'NewsItem' in your foreach loop.

     

    Sören

  • Sören Deger 733 posts 2844 karma points c-trib
    Aug 30, 2012 @ 17:59
    Sören Deger
    0

    They are document types with alias "NewsItem" under your current node? If yes, you must use Model instead of item in your foreach loop.

  • dominik 711 posts 733 karma points
    Aug 31, 2012 @ 08:37
    dominik
    0

    I have created a document type "News" with child items "News Items".  Now i want to show the latest created news.

    Here is the complete code

        foreach (dynamic item in Model.AncestorOrSelf().News)
        
         
           foreach (dynamic newsItem in item.NewsItems.Where("Visible").OrderBy("CreateDate desc"){

    }

    }

     

    Sorting still does not work

Please Sign in or register to post replies

Write your reply to:

Draft