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 35 posts 116 karma points
    1 week ago
    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 91 posts 437 karma points c-trib
    1 week ago
    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 35 posts 116 karma points
    1 week ago
    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 91 posts 437 karma points c-trib
    1 week ago
    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 35 posts 116 karma points
    1 week ago
    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 91 posts 437 karma points c-trib
    1 week ago
    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

Please Sign in or register to post replies

Write your reply to:

Draft