Help defining sort of PageFilter


Perhaps this is more of a Linq question but it has to do with sorting a PageList so I give it a shot. The following line sorts by a date (ExpDate = Expiration Date) first presenting pages that hasn't expired (= date passed) and is the supposed to sort them from A-Z. Today being the 4th of June an example of this would be first pages A-Z with an ExpDate > 4th of June and then A-Z with an ExpDate < 4th of June.

I'm not that good at Linq and the problem with the following is that it only sorts  the first part - the ones that has not yet expired - alphabetically and not the second part - the expired. Can anyone help me out and add what's missing so that they both sort from A-Z?

pages = pages.OrderByDescending(p => (DateTime.Parse(p.Property["ExpDate"].ToString()) < DateTime.Now) ? p.StartPublish.Date : DateTime.MaxValue).ThenBy(p => p.PageName);

Jun 04, 2013 9:15

What Property Type is your ExpDate? A PropertyDate?

A good rule is not to use p.Property["PropertyName"].ToString() or p["PropertyName"].ToString() since they can be null if the property does not exist or the value can be null.
See A good paranoia about this could be good.

Next I would have done something like this

pages = pages.OrderByDescending(p => ((DateTime) (p["ExpDate"] ?? DateTime.MinValue)) > DateTime.Now).ThenBy(p => p.PageName);

First you will order whether ExpDate is greater than current time is true or false, next by PageName.

At the moment I'm a little uncertain about if the correct implementation should be OrderBy/OrderByDescending and < or > with the datetime ordering but I hope this is a push in the right direction,

Edited, Jun 04, 2013 9:52

Thanks for the tip! I've seen it around and thought about the differences but always went with "this must be safer". Apparently not! :)

And thanks for the code as well. It worked right away!

Jun 04, 2013 10:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.