NoRM icon indicating copy to clipboard operation
NoRM copied to clipboard

Exception using complex where clause

Open cmenge opened this issue 14 years ago • 3 comments

I get the following exception when trying to do a rather complex query:

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

Query: var result = 
  Context.Query<Subscription>().
    Where(s => s.LastReportTicks + s.Interval * TicksPerDay < NowTicks).
    OrderBy(s => s.LastReportTicks + s.Interval * TicksPerDay);

at Norm.Linq.MongoQueryTranslator.HandleSort(Expression exp, OrderBy orderby) 
in MongoQueryTranslator.cs

The cast to (MemberExpression) assumes there's only a simple member access... I fiddled around a bit, but I was unable to fix this so far.

cmenge avatar May 02 '10 15:05 cmenge

Since the orderby above is not supported by MongoDB, I think that doing it for the caller on the client side transparently will lead to people thinking that they're executing a sort somewhere remotely and getting the results back. In this case, I recommend users add a ".AsEnumerable()" after the .Where(), as one would do for complex queries in LINQ-To-SQL

atheken avatar May 12 '10 11:05 atheken

I see your point. Still, we might want to provide a nicer exception - InvalidCast looks like a bug. Perhaps NotSupportedException would be better in this case?

cmenge avatar May 13 '10 15:05 cmenge

Sounds like a "has no supported translation" exception thrown by LINQ to SQL.

ghost avatar Aug 06 '10 23:08 ghost