C # Advanced Series--Dynamic Lamada (ii: Optimization)

Source: Internet
Author: User

Preface: A few days ago wrote a dynamic Lamada article C # Advanced Series-Dynamic Lamada, inspired by the xiao99 of the park friends, today intends to re-optimize the dynamic lamada of the tool class. Make a note here to avoid forgetting later.

First, the principle analysis

In the last chapter we said the use of dynamic Lamada and use of the scene, but the feeling is not easy to use in the project, the most difficult is to pass the property name of the string, it feels a bit too lower. Then it is the use of the enumeration really feel no need, we just need to contains, Equal, LessThan, GreaterThan and other methods to encapsulate a separate method can be. Well, say more easily make people dizzy, directly on the code bar.

Second, code example
 Public classLamadaextention<dto>whereDto:New ()    {        PrivateList<expression> m_lstexpression =NULL; PrivateParameterExpression M_parameter =NULL;  Publiclamadaextention () {m_lstexpression=NewList<expression>(); M_parameter= Expression.parameter (typeof(Dto),"x"); }
Read-only property that returns the generated Lamada PublicExpression<func<dto,BOOL>>Lamada {
         Get { returnGetlambda (); } } /// <summary> ///string contains filter/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidContains (Expression<func<dto,string>> Expproperty,Objectstrvalue) {Expression Expres= Expression.call (Expproperty.body,typeof(string). GetMethod ("Contains"), Expression.constant (strvalue)); M_lstexpression.add (Expres); } /// <summary> ///equals/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidEqual (Expression<func<dto,Object>> Expproperty,Objectstrvalue) { varmember =getmemberexpression (Expproperty); Expression Expres=expression.equal (Member, Expression.constant (strvalue, member. Type)); M_lstexpression.add (Expres); } /// <summary> ///less than/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidLessThan (Expression<func<dto,Object>> Expproperty,Objectstrvalue) { varmember =getmemberexpression (Expproperty); Expression Expres=Expression.lessthan (Member, Expression.constant (strvalue, member. Type)); M_lstexpression.add (Expres); } /// <summary> ///less than or equal/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidLessthanorequal (Expression<func<dto,Object>> Expproperty,Objectstrvalue) { varmember =getmemberexpression (Expproperty); Expression Expres=expression.lessthanorequal (Member, Expression.constant (strvalue, member. Type)); M_lstexpression.add (Expres); } /// <summary> ///Greater than/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidGreaterThan (Expression<func<dto,Object>> Expproperty,Objectstrvalue) { varmember =getmemberexpression (Expproperty); Expression Expres=Expression.greaterthan (Member, Expression.constant (strvalue, member. Type)); M_lstexpression.add (Expres); } /// <summary> ///greater than or equal/// </summary> /// <param name= "Expproperty" ></param> /// <param name= "strvalue" ></param> Public voidGreaterthanorequal (Expression<func<dto,Object>> Expproperty,Objectstrvalue) { varmember =getmemberexpression (Expproperty); Expression Expres=expression.greaterthanorequal (Member, Expression.constant (strvalue, member. Type)); M_lstexpression.add (Expres); }PrivateExpression<func<dto,BOOL>>Getlambda () {Expression whereexpr=NULL; foreach(varExprinch This. M_lstexpression) { if(whereexpr = =NULL) whereexpr =expr; Elsewhereexpr =Expression.and (whereexpr, expr); } if(whereexpr = =NULL) return NULL; returnExpression.lambda<func<dto, boolean>>(whereexpr, M_parameter); } //Get Memberexpression PrivateMemberexpression getmemberexpression (Expression<func<dto,Object>>exp) { varArrsplit = exp. Body.tostring (). Split ("(.)". ToCharArray (), stringsplitoptions.removeemptyentries); varStrproperty = Arrsplit[arrsplit.length-1]; Memberexpression member=Expression.propertyorfield (M_parameter, Strproperty); returnmember; } }

As can be seen, for common operations we encapsulate the contains, Equal, LessThan, Lessthanorequal, GreaterThan, greaterthanorequal six methods, In addition to the parameters of the contains method directly using the Expression<func<dto, string>> type thought, the other used Expression<func<dto, object>>. Because the contains method can only be a string type of variable operation, and other operations may involve other types, is to pass the object type, there is a problem Bo master debugging for a long time, because the pass is object, this to get the real type of attribute is not so easy, I haven't found it for a long time. Finally, only by getmemberexpression this method to get memberexpression.

Let's take a look at how to use:

 public  object  getusers (int  limit, int  offset, string  Username, string   FullName) { Span style= "color: #0000ff;" >var  olamadaextention = new  lamadaextention<dto_tr_sys_users>< Span style= "color: #000000;"            >(); Olamadaextention.equal (x = x.user_name, username                     Olamadaextention.lessthan (x => X.modifytime, datetime.now);  var  lstres = Usermanager.find ( Olamadaextention.lamada).     ToList (); }

The biggest convenience is that we want to filter the field can be Lamada point out, and then look at the previous usage

Olamadaextention.getexpression ("user_name", username, expressiontype.contains);

There was no instant on the tall. It's better to user_name a direct point than to knock a string. Thanks to the magical Lamada, thanks to the Almighty C #, thanks to the enthusiastic friends of the park.

C # Advanced Series--Dynamic Lamada (ii: Optimization)

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.