using Expression Trees for Filtering Logic

26 views
Skip to first unread message

Shubha Lakshmi

unread,
Sep 21, 2017, 4:40:31 AM9/21/17
to RavenDB - 2nd generation document database
I am testing an expression Tree based filtering logic in RavenDB, something Like this :

               
string propertyName = "FirstName";
 string value = "Steven";

var
type = typeof(Employee);
 
var param = Expression.Parameter(type,"e");
 
 
var propertyRef = Expression.Property(param,propertyName);
 
var constantRef = Expression.Constant(value);
       
 
var expr = Expression.Lambda<Func<Employee, bool>>(Expression.Equal(propertyRef, constantRef), new[] { param});
 
 
var propertyRef1 = Expression.Property(param,"LastName");
 
var constantRef1  = Expression.Constant("Buchanan");


 
var rightExp = Expression.Lambda<Func<Employee, bool>>(Expression.Equal(propertyRef1, constantRef1), new[] { param });
 
var exprCombined = Expression.Lambda<Func<Employee, bool>>(Expression.And(expr.Body, rightExp.Body), new[] { param});



var employees = session.Query<Employee>().Where(exprCombined);   // Query 1

var employees2 = session.Query<Employee>().Where(t=>t.FirstName=="Steven"&&t.LastName=="Buchanan").Dump();   //Query 2
 
Here, Query 1 does not work, but Query 2 does. Does RavenDB does not process the And clause in the Final Expression Tree. An Explanation would really help.

Thanks.

Oren Eini (Ayende Rahien)

unread,
Sep 21, 2017, 4:53:46 AM9/21/17
to ravendb
You are not really doing the same thing.
rightExp.Body != Expression.Equal(propertyRef1, constantRef1), for example. 

Try looking directly at the different ASTs generates, and try building the expression directly.

At any rate, you shouldn't be building this like this. There is the DocumentQuery just for dynamic stuff like that.

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.

Shubha Lakshmi

unread,
Sep 21, 2017, 5:40:21 AM9/21/17
to RavenDB - 2nd generation document database
Hi Oren,
Thanks for your reply. I am correcting my code as :

             
 string propertyName = "FirstName";
 
string value = "Steven";
 
 
var type = typeof(Employee);
 
var param = Expression.Parameter(type,"e");
 
 
var propertyRef = Expression.Property(param,propertyName);
 
var constantRef = Expression.Constant(value);

       
 
var expr = Expression.Equal(propertyRef, constantRef);

 
 
var propertyRef1 = Expression.Property(param,"LastName");
 
var constantRef1  = Expression.Constant("Buchanan");



 
var rightExp =Expression.Equal(propertyRef1, constantRef1);
 
var exprCombined = Expression.Lambda<Func<Employee, bool>>(Expression.And(expr, rightExp), new[] { param});
 

But this Query =>
var employees = session.Query<Employee>().Where(exprCombined);
Still does not give the desired result. Could you please suggest what could have gone wrong ..Also, thanks for your suggestion regarding DocumentQuery API for building dynamic search clauses. I will try working with it too.

Oren Eini (Ayende Rahien)

unread,
Sep 21, 2017, 5:48:11 AM9/21/17
to ravendb
What is the differences between this and the compiler generated AST?

At a guess, you are using Expressison.And and need Expression.Andalso

Hibernating Rhinos Ltd  

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

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

 


--

Shubha Lakshmi

unread,
Sep 21, 2017, 6:13:30 AM9/21/17
to RavenDB - 2nd generation document database
Thanks Oren!
   Using AndAlso works .. If by AST you mean run-time generated code from the expression , using AndAlso generates this :
e => ((e.FirstName == "Steven") AndAlso (e.LastName == "Buchanan"))



On Thursday, September 21, 2017 at 2:10:31 PM UTC+5:30, Shubha Lakshmi wrote:

Oren Eini (Ayende Rahien)

unread,
Sep 21, 2017, 6:18:57 AM9/21/17
to ravendb
Yes

Hibernating Rhinos Ltd  

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

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

 


--
Reply all
Reply to author
Forward
0 new messages