Expression tree Dynamic splicing lambda

Source: Internet
Author: User

Objective

Recently in the optimization of the code written by colleagues (our framework is DAPPERLAMBDA), there is a very common scenario-the interface provides a number of query conditions box for users to filter data. Since Dapperlambda conditionally query is passed in the expression tree parameters, such as where the query condition has an audit status, and the other five is a fuzzy query, then this query when the expression tree parameters will be written two times, so that the code appears to have a lot of repetition, and if the query conditions are many cases, Writing that expression tree parameter is also easy to write or write incorrectly. So I was thinking that if you can dynamically splice this expression tree, the code will be much leaner.

Body

Perhaps my above text description lets you not be aware of, that below I is with the simple code to explain this question again:

1 Expression<func<sysuser, bool>> exp1 = s = = S.username.contains ("1") && s.age > 10;2 expressio N<func<sysuser, bool>> exp2 = s = S.username.contains ("1") && s.age > && s.isenable = = 1;3 using (var context = new DbContext (). ConnectionString (connstring)) 4 {5     var result1 = context. Select<sysuser> (EXP1). Querymany (); 6     var result2 = context. Select<sysuser> (EXP2). Querymany (); 7}

The above code two queries, the result of the first query results RESULT1 is the user name contains 1 and the age is greater than 10, and the result result2 is in the query results RESULT1 conditions on the condition of the account entry. So we see that the expression trees of exp1 and exp2 are mostly the same, and in this case it seems to be acceptable to repeat the code, but in the real world, the duplicated code is more than that. Because Exp2 and EXP1 compared, exp2 just above the EXP1 added a condition, then we have no way to add a condition on the basis of EXP1 to the EXP2? The answer is of course possible.

 Expression Extension class:

1 public static class Expressionext 2 {3 public     static expression<func<t, bool>> and<t> (This expres Sion<func<t, bool>> expr1, expression<func<t, bool>> expr2) 4     {           5         return Expression.lambda<func<t, Bool>> (Expression.andalso (expr1. Body, Expr2. Body), Expr1. Parameters); 6     } 7 public     static expression<func<t, bool>> or<t> (this expression<func<t, bool> > expr1,expression<func<t, bool>> expr2) 8     {9         return expression.lambda<func<t, bool> > (Expression.orelse (expr1. Body, Expr2. Body), Expr1. Parameters);     }11}

The code above is at the heart of our article, and I'm here to explain how this and extension method is done, first using the static method in the expression class AndAlso the body of Expr1 and expr2 together, If the AndAlso method returns a result of type binaryexpression, and the Dapperlambda condition parameter requires a lambda expression tree, So here we need to create a lambda expression tree using the Expression.lambda method to construct a delegate type.

Now let's refine our initial example by extending the method above:

1 Expression<func<sysuser, bool>> exp1 = s = S.username.contains ("1") && s.age > 0;2 Expression <func<sysuser, bool>> exp2 =exp1. and (s = = S.isenable = 1); 3 using (var context = new DbContext (). ConnectionString (connstring)) 4 {5     var result1 = context. Select<sysuser> (EXP1). Querymany (); 6     var result2 = context. Select<sysuser> (EXP2). Querymany (); 7}
Conclusion

Although this extension method is only a few lines of code, but if the few lines of code, in our code will probably be dozens of lines, hundreds of lines of code. Sometimes the key to solving the problem is a humble thing, but it is such a humble thing can help us solve some big problems.

Speaking of this, reminds me of a word--every bit of learning a little more, less write a line of code.

Expression tree Dynamic splicing lambda

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.