Dynamic filter expressions in an OpenAccess LINQ query

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Fri, 16 Apr 2010 00:00:00 GMT Indexed on 2010/04/16 16:43 UTC
Read the original article Hit count: 300

Filed under:

We had some support questions recently where our customers had the need to combine multiple smaller predicate expressions with either an OR or an AND  logical operators (these will be the || and && operators if you are using C#). And because the code from the answer that we sent to these customers is very interesting, and can easily be refactorred into something reusable, we decided to write this blog post.

The key thing that one must know is that if you want your predicate to be translated by OpenAccess ORM to SQL and executed on the server you must have a LINQ Expression that is not compiled. So, let’s say that you have these smaller predicate expressions:

Expression<Func<Customer,  bool>> filter1 = c => c.City.StartsWith("S");
Expression<Func<Customer, bool>> filter2 = c => c.City.StartsWith("M");
Expression<Func<Customer, bool>> filter3 = c => c.ContactTitle == "Owner";

And ...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner