Dynamic Where Predicate not working

25 views
Skip to first unread message

Ian Davies

unread,
Jun 20, 2017, 10:14:24 AM6/20/17
to RavenDB - 2nd generation document database
Hi Guys 

Having a strange issue with the RavenDbClient when applying a sort predicate

I need to be able to have a Where predicate based on a sort expression where the field and field type are dynamic with greater than or less than.

To do it I have this method

public static Expression<Func<TWhere, bool>> DynamicGreaterOrLessThanWhere<TWhere, TValue>(string propertyName, TValue value, bool greaterThan)
       
{
           
var argParam = Expression.Parameter(typeof(TWhere), "i");


           
var orderProperty = Expression.Property(argParam, propertyName);
           
var expressionValue = Expression.Constant(value);


           
BinaryExpression expression = null;


           
if (orderProperty.Type.IsNullableType())
           
{
               
var orderPropertyNotNullable = Expression.Convert(orderProperty, expressionValue.Type);
                expression
= greaterThan
                                 
? Expression.GreaterThan(orderPropertyNotNullable, expressionValue)
                                 
: Expression.LessThan(orderPropertyNotNullable, expressionValue);
           
}
           
else
           
{
                expression
= greaterThan ? Expression.GreaterThan(orderProperty, expressionValue) : Expression.LessThan(orderProperty, expressionValue);
           
}


           
return Expression.Lambda<Func<TWhere, bool>>(expression, argParam);
       
}

And I use it the following way

 if (searchCriteria.Direction.Equals(StorageConstants.Pagination.Newer, StringComparison.CurrentCultureIgnoreCase))
           
{
               
var sortPredicate = DynamicGreaterOrLessThanWhere<IInsightSearch, TSort>(sortProperty.Name, entryPointValue, true);
                query
= query.Where(sortPredicate) ;
           
}

When I run it I get this as the result of the sortPredicate is 

Body = {(i.PublishDate > 19-Jun-17 12:10:00 PM)}


Debug view of the sortPredicate 

$i.PublishDate > .Constant<System.DateTime>(19-Jun-17 12:10:00 PM)

But once I apply this to the query (Note Query is type of RavenQueryInspector)

query = query.Where(sortPredicate) ;

The value of query is this ??? NOTE :the "TO NULL"

 {PublishDate:{2017-06-19T12:10:00.0000000 TO NULL}}

Debug View of the Query is this 

new System.Linq.Expressions.Expression.MethodCallExpressionProxy(((System.Linq.IQueryable)query).Expression).DebugView
.Call System.Linq.Queryable.Where(
    .Constant<Raven.Client.Linq.RavenQueryInspector`1[Oak.Repository.Interfaces.IInsightSearch]>(),
    '(.Lambda #Lambda1<System.Func`2[Oak.Repository.Interfaces.IInsightSearch,System.Boolean]>))

.Lambda #Lambda1<System.Func`2[Oak.Repository.Interfaces.IInsightSearch,System.Boolean]>(Oak.Repository.Interfaces.IInsightSearch $i)
{
    $i.PublishDate > .Constant<System.DateTime>(19-Jun-17 12:10:00 PM)
}

When the query is executed I do  get items with null values. Please also note that I have a live feed provider further down the line also appends PublishDate != null to the query which seems to be ignored.

Any ideas on why this is happening or if anyone has a better solution on how else I can achieve this would be very much appreciated.

Thanks in Advance





Oren Eini (Ayende Rahien)

unread,
Jun 20, 2017, 1:50:46 PM6/20/17
to ravendb
This is how we represent range query for Lucene:
 {PublishDate:{2017-06-19T12:10:00.0000000 TO NULL}}

Depending on your query complexity, it might be that you are ORing something with this query.
You can add a && PublishDate != null to your dynamic clause

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages