Not sure if this is possible, but I need to have an OrderBy statement on a node collection (Typed Content in this case, Descendants) that orders by a property if it exists, else order by the published date.
So, my chil nodes can be one of a couple of DocTypes, some have a "date" property, others do not, depending on the type.
It'll either be something simple that I'm missing, or it really isn't possible and I'll have to do some kind of lengthy workaround..
If I understand your question correct you want to sort a collection on nodes, of a property if it has the property else it should sort of a other value, Maybe you would use this syntaxs to get what you want.
@{ <ul> @* OrderBy() takes the property to sort by *@
First I sort on a custom property called publish data, the data type is a date picker, then sort by name, and the sort order is descending. In my case I sort some news items and basic textpages, The result of this is that the news item where the date is set, is displayed first, and then the text pages.
I think you can do this Mark, given you want to order by a date field in both cases - which might be what you need. I may have misread but I think the example Dennis gives would order all your items that don't have the property either at the top or bottom of the list, which doesn't sound like what you wanted.
So basically as above, but instead use this as the order by clause:
.OrderBy(x => x.HasProperty("publishDate") ? x.GetPropertyValue<DateTime>("publishDate") : x.CreateDate) // or x.UpdateDate
Will confess not tested, but looks like this should work.
That is exactly what I was looking for, obvious now I look at it, especially as I'm certain I have done it before in a previous project! It does indeed work, so much appreciated. In fact, I have just put it back into an older project where I have a number of date fields to check against and chaining the query works a charm (so far...!)
Dennis, appologies, Andy was right, that does indeed put things in groups that appear out of order, so not really what I wa after, thank you kindly for your thoughts though.
DonĀ“t apology, I am happy that you found I solution to your case. I think that we all just try to help, as much as we can, the important thing here is that you found a solution to your problem, this time with help from Andy.
OrderBy depending on if property exists
Hi Guys,
Not sure if this is possible, but I need to have an OrderBy statement on a node collection (Typed Content in this case, Descendants) that orders by a property if it exists, else order by the published date.
So, my chil nodes can be one of a couple of DocTypes, some have a "date" property, others do not, depending on the type.
It'll either be something simple that I'm missing, or it really isn't possible and I'll have to do some kind of lengthy workaround..
Any help would be appreciated though :-)
Mark
Hi Mark,
If I understand your question correct you want to sort a collection on nodes, of a property if it has the property else it should sort of a other value, Maybe you would use this syntaxs to get what you want.
First I sort on a custom property called publish data, the data type is a date picker, then sort by name, and the sort order is descending. In my case I sort some news items and basic textpages, The result of this is that the news item where the date is set, is displayed first, and then the text pages.
Hope this helps,
/Dennis
I think you can do this Mark, given you want to order by a date field in both cases - which might be what you need. I may have misread but I think the example Dennis gives would order all your items that don't have the property either at the top or bottom of the list, which doesn't sound like what you wanted.
So basically as above, but instead use this as the order by clause:
Will confess not tested, but looks like this should work.
Andy
Andy,
That is exactly what I was looking for, obvious now I look at it, especially as I'm certain I have done it before in a previous project! It does indeed work, so much appreciated. In fact, I have just put it back into an older project where I have a number of date fields to check against and chaining the query works a charm (so far...!)
Dennis, appologies, Andy was right, that does indeed put things in groups that appear out of order, so not really what I wa after, thank you kindly for your thoughts though.
Thanks again guys,
Mark
Hi Mark,
DonĀ“t apology, I am happy that you found I solution to your case. I think that we all just try to help, as much as we can, the important thing here is that you found a solution to your problem, this time with help from Andy.
/Dennis
is working on a reply...