dynamic WHERE clause in linq

Apr 18, 2011 at 6:11 PM

i can successfully define a where clause and pass it into a query using the following :

Expression<Func<MessageQuery, bool>> where = x =>  x.RecipientUserId == physician.UserId &&  x.ViewedDate == null &&  x.SenderUserId != null &&

 MessageCollection.runselect(where);

 

Is there a way to dynamically define the elements in the where clause  (as string elements) and pass it into the runselect statement , for example

where.Add("RecipientUserId" , "==1");

where.Add("SenderUserId" , "<>2");

where.Add("TotalAmount" , ">2000");

MessageCollection.runselect(where);

and linq would then compile it to give back the exact same result as if it was defined in a typed manner.

if it cannot be done out of the box with nhydrate can we use the  linq dynamic query library to build custom where clause (using strings)  as shown in following blog post 

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

giannis

 

 

 

Apr 18, 2011 at 10:19 PM

 I think  this pattern  called by  QueryObject by Martin Fauler.      http://martinfowler.com/eaaCatalog/queryObject.html

i also think it can be good feature for framework.  

Coordinator
Apr 23, 2011 at 5:03 PM

There are people who build custom code to do all sorts of LINQ manipulation but you will need to research these. This is beyond the capabilities or scope of the nHydrate project.

Coordinator
May 2, 2011 at 10:52 PM

Just to be clear, the "where" parameter can take an object of type "Expression<Func<ENTITYbool>>". You can also chain these statements together using some LINQ libraries; however this is not the scope of this project. You will need to use some code to concatenate LIQN statements together before you pass them into the RunSelect method. This is more LINQ technology and not ORM specific.

May 3, 2011 at 5:39 AM
Edited May 3, 2011 at 9:31 AM

yes , you are right.

after doing some research I've managed to use linq expression trees to build dynamically the where clause.

nhydrate has made me  like linq more than i did previouslly.

giannis