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