Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1289 posts 2746 karma points
    Oct 13, 2014 @ 21:35
    Amir Khan
    0

    Odd results with OrderBy and Take

    Hi,

    I have this bit of razor in a partial view that is going through and making a list of nodes. For some reason, if I add "Take(3)" or any other number, it returns no results...Without the take stament it returns 6 items.

    Any idea why this would be?

    -Amir

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    var root = CurrentPage.AncestorOrSelf();
    var nodes = root.Descendants("Event");
    string switchStatus = @CurrentPage.calendarFeed.ToString();
    }

    @{

    <section class="feed calendar">
    <nav>
    <ul>

    @foreach(var page in nodes.OrderBy("eventStartDateTime").Take(3)){
    var eventDate = @page.EventStartDateTime;
    var today = DateTime.Now;
    <span></span>
    if(eventDate > today) {
    <li>
    <time><h2>@page.eventStartDateTime.ToString("MMMM-dd-yyy hh:mm tt")</h2></time>
    <a href="@page.Url">@page.EventTitle</a>
    </li>

    }

    }
    </ul>
    </nav>
    </section>
    <a href="/events/" class="arrow green">More Events</a>
    }
  • Amir Khan 1289 posts 2746 karma points
    Oct 13, 2014 @ 22:09
    Amir Khan
    0

    Well, apparently I was just approaching this a bit backward. OrderBy desc and Skip(3) gave me the desired results.

    -Amir

  • Charles Afford 1163 posts 1709 karma points
    Nov 13, 2014 @ 23:23
    Charles Afford
    0

    Yep OrderBy des and then Skip(3) should do it, How are you asserting that the first 3 in that collection are the ones you want to skip? seems a bit risky :/. Charlie

  • Amir Khan 1289 posts 2746 karma points
    Nov 14, 2014 @ 21:03
    Amir Khan
    0

    I wasn't, I'm actually still not sure what the best way to approach this is. Any suggestions?

     

    -Amir

  • Charles Afford 1163 posts 1709 karma points
    Nov 20, 2014 @ 23:51
    Charles Afford
    0

    Hi Amir Just reading this again. Sorry for the late reply. I am not getting notifications an been busy

  • Charles Afford 1163 posts 1709 karma points
    Nov 21, 2014 @ 00:01
    Charles Afford
    0

    Have you debugged this?

    The IEnumerable

    so nodes.Count() should be > 0

    if we do nodes.Take(3) should give you the first three items in the collection.

    The reason i think you are having problems is because of:

    eventStartDateTime

    What you are trying to do i orderBy eventStartDateTime on an object not a property of an object if that makes sense.

    You will need something like OrderBy(x=>x.properties).Where(x=>x.Alias == eventStartDateTime)

    Something like that. I will try an give you a working example tomorrow morning.

    Hope this helps.

    Thanks,

    Charlie :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies