This project is read-only.

dynamic WHERE clause in linq

Apr 18, 2011 at 7: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 &&



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");


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





Apr 18, 2011 at 11:19 PM

 I think  this pattern  called by  QueryObject by Martin Fauler.

i also think it can be good feature for framework.  

Apr 23, 2011 at 6: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.

May 2, 2011 at 11: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 6:39 AM
Edited May 3, 2011 at 10: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.