Copied to clipboard

Flag this post as spam?

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


  • Jeremy Coulson 61 posts 143 karma points
    Jul 11, 2018 @ 14:29
    Jeremy Coulson
    0

    Why does OrderByDescending fail?

    When using the following code to try to get the ID of the newest blog entry on our site, I get the error that "at least one object must implement icomparable". If I remove the .OrderByDescending(z => z.GetProperty("uBlogsyPostDate")) from the code, it works but I get the oldest blog entry and not the latest.

    Here's the code:

    int postNode = Umbraco.TypedContent(1052)
    .DescendantsOrSelf("uBlogsyPost")
    .Where(x => x.GetProperty("umbracoNaviHide").Value != "1")
    .OrderByDescending(z => z.GetProperty("uBlogsyPostDate"))
    .Take(1)
    .ToList()
    .Select(y => y.Id).FirstOrDefault();
    

    I have to admit that I'm generally lost when it comes to LINQ, so I usually just keep throwing stuff around until something works.

    Thanks!

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 11, 2018 @ 19:25
    Søren Gregersen
    100

    Hi,

    The GetProperty-method returns a property, not the value. You may need to do:

    var latestPost = Umbraco.TypedContent(1052)
        .DescendantsOrSelf("uBlogsyPost")
        .Where(x => x.GetProperty("umbracoNaviHide").Value != "1")
        .OrderByDescending(z => z.GetPropertyValue<DateTime>("uBlogsyPostDate"))
        .FirstOrDefault();
    
    var latestPostId = (latestPost==null? -1 : latestPost.Id)
    
  • Jeremy Coulson 61 posts 143 karma points
    Jul 11, 2018 @ 19:44
    Jeremy Coulson
    1

    I ended up doing this, but I'll try your idea tonight and see if it works too. It might be better.

    Thanks!

    int postNode = Umbraco.TypedContent(1052)
                                    .Descendants("uBlogsyPost")
                                    .Where(x => x.GetProperty("umbracoNaviHide").Value != "1")
                                    .OrderBy("uBlogsyPostDate desc")
                                    .Take(1)
                                    .Select(y => y.Id)
                                    .FirstOrDefault();
    
  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 11, 2018 @ 22:31
    Søren Gregersen
    0

    At least you don't need to Take(1) since you only take the first :)

    update: for performance, you should avoid the Descendants() call - this would perform much better if implemented as a (examine) search instead :)

  • Jeremy Coulson 61 posts 143 karma points
    Jul 12, 2018 @ 02:05
    Jeremy Coulson
    0

    Hey, that worked great. I prefer it because it's more consistent with the rest of the code.

    Do you have an example of how to do this with Examine? I basically grabbed some code that was already in our CMS and riffed on that to write this.

    Thanks a ton!

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 12, 2018 @ 11:18
    Søren Gregersen
    2
    var criteria = ExamineManager.Instance.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content)
    // add criteria to only look for documents of a specific type
    .NodeTypeAlias("uBlogsyPost").And()
    // add criteria to just look a posts below a certain node
    .GroupedOr(new[] { "parentID" }, new[]{ 1052 } );
    
    var latestPostId = ExamineManager.Instance.Search(criteria)
        .Select(x => new {
            x.Id,
            Date = DateTime.ParseExact(x.Fields["uBlogsyPostDate"], "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)
        })
        .OrderByDescending(x=>x.Date)
        .Select(x=>x.Id)
        .FirstOrDefault();
    

    I'll be something like above :)

    It is possible to do the sorting in examine also, but in the place I used the code, the data could come from two different fields - just removed that for readability.

    The examine wiki is a good starting point: https://github.com/Shazwazza/Examine/wiki

  • 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