Util application Framework Common operations Class (11): Expression Builder

Source: Internet
Author: User

This article describes the Expression builder, which is used to dynamically create an expression.

In the Util Project Lambdas directory, add the ExpressionBuildercode as follows.

usingSystem;usingSystem.Linq.Expressions;namespaceUtil.lambdas {/// <summary>    ///Expression Builder/// </summary>     Public classExpressionbuilder<tentity> {        /// <summary>        ///initializing an expression builder/// </summary>         PublicExpressionBuilder () {Parameter= Expression.parameter (typeof(TEntity),"T" ); }        /// <summary>        ///Parameters/// </summary>        PrivateParameterExpression Parameter {Get;Set; } /// <summary>        ///Get Parameters/// </summary>         Publicparameterexpression getparameter () {returnParameter; }        /// <summary>        ///Create an expression/// </summary>        /// <param name= "Property" >Property Expressions</param>        /// <param name= "operator" >operator</param>        /// <param name= "value" >value</param>         PublicExpression create<t> (expression<func<tentity, t>> property, Operator @operator,Objectvalue) {            returnParameter.property (Lambda.getmember (property)).        Operation (@operator, value); }        /// <summary>        ///Convert to lambda expression/// </summary>        /// <param name= "expression" >An expression</param>         PublicExpression<func<tentity,BOOL>>tolambda (expression expression) {if(Expression = =NULL )                return NULL; returnExpression. Tolambda<func<tentity,BOOL>>(Parameter); }    }}

In the Util.tests test project, add the expressionbuildertest unit test, as shown in the code below.

usingSystem;usingSystem.Linq.Expressions;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingUtil.lambdas;usingUtil.Tests.Samples;namespaceutil.tests {/// <summary>    ///test An expression builder/// </summary>[TestClass] Public classExpressionbuildertest {/// <summary>        ///Expression Builder/// </summary>Expressionbuilder<test2>_builder; /// <summary>        ///Test Initialization/// </summary>[TestInitialize] Public voidTestinit () {_builder=NewExpressionbuilder<test2>(); }        /// <summary>        ///Create an expression/// </summary>[TestMethod] Public voidTestcreate_int () {Expression<func<test2,int>> Property = T + =T.int; varexpression = _builder. Create (property, Operator.equal,1 ); Expression<func<test2,BOOL>> expected = T = = T.int = =1; Assert.AreEqual (expected. ToString (), _builder. TOLAMBDA (expression).        ToString ()); }        /// <summary>        ///Create an expression/// </summary>[TestMethod] Public voidtestcreate_int_nullable () {Expression<func<test2,int?>> Property = T + =T.nullableint; varexpression = _builder. Create (property, Operator.equal,1 ); Assert.AreEqual ("T = (T.nullableint = = 1)", _builder. TOLAMBDA (expression).        ToString ()); }    }}

Since the current function is relatively simple, only two methods have been added, and then extended after the need.

The preparation is ready, and the next one can describe the query criteria and date range, and the value range criteria encapsulation.

. NET Application Framework Exchange QQ Group: 386092459, welcome interested friends to join the discussion.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/xiadao521/

: Http://files.cnblogs.com/xiadao521/Util.2015.1.6.1.rar

Util application Framework Common operations Class (11): Expression Builder

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.