mfenniak / rethinkdb-net

A C# / .NET client driver for RethinkDB.
Other
247 stars 37 forks source link

Valid DateTime Query Expression #223

Closed cecilphillip closed 9 years ago

cecilphillip commented 9 years ago

I'm working on the query I originally posted in #222.

            var today = DateTime.Now.Date;
            var query = _table.Group(evt => evt.EventType)
                .Map(e => new
                {
                    count =1, 
                    today = e.DateCreated.Date.Equals(today) ? 1 : 0,
                    month = e.DateCreated.Month.Equals(today.Month) ? 1 : 0
                });
            var result = conn.Run(query);

my date expressions don't seem to be valid/

Failed to perform client-side evaluation of expression tree node 'e.DateCreated.Date.Equals(value(RethinkToss.Program+<>c__DisplayClass0).today)'; this is caused by referring to a server-side variable in an expression tree that isn't convertible to ReQL logic"

Could you advise/direct me to what the allowable DateTime expressions are ?

mfenniak commented 9 years ago

Hm, yeah. So, here's what's happening -- when you write something like "e.DateCreated.Date.Equals(today)", rethinkdb-net needs to transform that into a server-side query expression. The problem is that the library doesn't currently know how to convert the "Date" property on the DateTime type into a server-side expression, so, it tries the next best thing which is to evaluate it client-side, but then it can't do that because it doesn't know the value of "e.DateCreated" on the client-side.

The short story is that you can probably avoid this error by using .Year, .Month, and .Day fields on the DateTime rather than .Date. All of those properties will work.

How we convert the expressions into server-side code for DateTime objects is defined here: https://github.com/mfenniak/rethinkdb-net/blob/master/rethinkdb-net/Expressions/DateTimeExpressionConverters.cs, so you can see what properties and methods are supported.

cecilphillip commented 9 years ago

Thanks again