OrderByDescending in version 8 with custom date field
I have this code in Version 7 to display news items by a custom date field but when I do this in Version 8 it breaks, any ideas what I'm doing wrong? Apologies my Linq & Lambda knowledge is very poor :(
var items = Model.Content.Site().FirstChild("news").Children("newsItems").Where(x => x.IsVisible()).OrderBy("newsDateTime descending");
Version 8 - Custom date field alias: newsDate (I'm also picking the starting parent from the tree in the variable 'guid'), this works with x.CreatedDate but I really need to do this from a user-defined field.
var guid = @Model.Value("repeaterGUID");
var selection = Umbraco.Content(Guid.Parse(@guid.ToString()))
.Children()
.Where(x => x.IsVisible())
.OrderByDescending(x => x.Model.Value("newsDate"));
I think you just need to use x.Model.Value<DateTime>("newsDate") so that OrderByDescending knows that it's sorting dates.
Although Model.Value("newsDate") will be returning DateTimes, as far as OrderByDescending knows it could return any object. It doesn't know that they all happen to be DateTimes, and since it doesn't know how to compare object, it breaks.
Thanks, Steve really appreciate you taking the time to reply. I had tried this previously and just again now, but I still get an error, so I think it could be the version of 8 I am using (Umbraco version 8.0.2) which is the issue as the error seems unrelated to the task at hand.
OrderByDescending in version 8 with custom date field
I have this code in Version 7 to display news items by a custom date field but when I do this in Version 8 it breaks, any ideas what I'm doing wrong? Apologies my Linq & Lambda knowledge is very poor :(
Version 8 - Custom date field alias: newsDate (I'm also picking the starting parent from the tree in the variable 'guid'), this works with x.CreatedDate but I really need to do this from a user-defined field.
I think you just need to use
x.Model.Value<DateTime>("newsDate")
so thatOrderByDescending
knows that it's sorting dates.Although
Model.Value("newsDate")
will be returning DateTimes, as far as OrderByDescending knows it could return anyobject
. It doesn't know that they all happen to be DateTimes, and since it doesn't know how to compareobject
, it breaks.Thanks, Steve really appreciate you taking the time to reply. I had tried this previously and just again now, but I still get an error, so I think it could be the version of 8 I am using (Umbraco version 8.0.2) which is the issue as the error seems unrelated to the task at hand.
Oops, I skipped straight to the sorting and forgot that
Children
is a property now. Try this:Perfect!! A million thanks!!
is working on a reply...