Orderby Nullable DateTime

930 views
Skip to first unread message

devm...@hotmail.com

unread,
Dec 29, 2011, 5:32:31 AM12/29/11
to rav...@googlegroups.com
Hi, 
what is the right way to order query based on Datetime fields with nullable values possible? 

i am doing this but i am getting the usual error.

usersList = RavenSession.Query<User>().OrderByDescending
                                                (
i => i.RegisterDate.HasValue? i. RegisterDate.Value.Date:DateTime.MaxValue 
);

Unable to cast object of type 'System.Linq.Expressions.FullConditionalExpression' to type 'System.Linq.Expressions.MemberExpression'. 

and if i do this, i get wrong order, where nullable values are not being ordered as last

usersList = RavenSession.Query<User>().OrderByDescending
                                                (
i => i. RegisterDate.Value.Date
);

thanks in advanced.

Oren Eini (Ayende Rahien)

unread,
Dec 29, 2011, 7:22:37 AM12/29/11
to rav...@googlegroups.com
You are trying to do computations again. 
This will work:

usersList = RavenSession.Query<User>().OrderByDescending ( i => i. RegisterDate );

Fitzchak Yitzchaki

unread,
Dec 29, 2011, 7:56:04 AM12/29/11
to rav...@googlegroups.com, devm...@hotmail.com
You have basically two options here:

1.  RegisterDate would not be nullable in the database, but will be the DateTime.MaxValue instead.
2. Create an index that will project the nullable RegisterDate to a non nullable one.

The two options are essentially the same. Don't do computation in the client. Or predefined your data in the way that you want it, or project is using an index.

Oren Eini (Ayende Rahien)

unread,
Dec 29, 2011, 7:57:28 AM12/29/11
to rav...@googlegroups.com, devm...@hotmail.com
Note that by default, RavenDB will handle it properly, and sort null values after all normal date values.

devm...@hotmail.com

unread,
Dec 29, 2011, 9:25:09 AM12/29/11
to rav...@googlegroups.com, devm...@hotmail.com
thanks both for fast answer, you are saving my face, as this is project going to be the prove for my group that raven is the way to go, and alot of things gets clearer with your replies,

@Fitzchak Yitzchaki

thanks for the advice, but i don't think DateTime.MaxValue, is an option, as it will show up for user and he will get confused.

@Ayende

i dont know if it is my mistake or there is a bug, but if i do 

var usersList= RavenSession.Query<User>().OrderByDescending(i=>i.RegisterDate.Value.Date);

i get the null values ordered before none null ones.

if i do 
var usersList= RavenSession.Query<User>();

i get them in right order and null values show last, but i want it to be based ordered based on the Date part only and not Date and Time Parts .

thank again

Oren Eini (Ayende Rahien)

unread,
Dec 29, 2011, 10:44:40 AM12/29/11
to rav...@googlegroups.com, devm...@hotmail.com
As I said, use:
var usersList= RavenSession.Query<User>().OrderByDescending(i=>i.RegisterDate)

devm...@hotmail.com

unread,
Jan 1, 2012, 7:42:15 AM1/1/12
to rav...@googlegroups.com, devm...@hotmail.com
thanks all , 
to see my complete solution for my problem 
Reply all
Reply to author
Forward
0 new messages