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.
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.
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:
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!
Hi,
The
GetProperty
-method returns a property, not the value. You may need to do:I ended up doing this, but I'll try your idea tonight and see if it works too. It might be better.
Thanks!
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 :)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!
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
is working on a reply...