Dynamic construction of lambda expressions

Source: Internet
Author: User

Environment: Visual Studio2008. NET Framework3.5

Situation: Query condition is uncertain, it needs to be generated dynamically

Dynamic Construction Code:

1/**////<summary>


2///Dynamic construction of lambda expressions


3///</summary>


4///<typeparam name= "T" > Query target entity </typeparam>


5 public class constructlambda<t> where T:class, new ()


6 {


7 private Type ttype;


8/**////<summary>


9///Construction Method


///</summary>


public Constructlambda ()


12 {


ttype = typeof (T);


14}


/**////<summary>


16///structure and expression


///</summary>


///<param name= "dictionary" > Tectonic source </param>


///<RETURNS>LAMBDA expression </returns>


public expression<func<t, bool>> getandlambdaexpression (dictionary<string, string> dicti Onary)


21 {


Expression Expression_return = Expression.constant (true);


parameterexpression Expression_param = Expression.parameter (Ttype, "P");


foreach (String key in dictionary. Keys)


25 {


Expression temp = expression.equal (Expression.call expression.property (Expression_param, Ttype.get Property (key)), Ttype.getmethod ("ToString"),


expression.constant (Dictionary[key]));


Expression_return = Expression.and (Expression_return, temp);


29}


Return (expression<func<t, bool>>) expression.lambda<func<t, bool>> (Expression_return , new parameterexpression[] {expression_param});


31}


32


/**////<summary>


34///construction or expression


///</summary>


///<param name= "dictionary" > Tectonic source </param>


Notoginseng///<returns>lambda expression </returns>


public expression<func<t, bool>> getorlambdaexpression (dictionary<string, string> dictio nary)


39 {


Expression Expression_return = Expression.constant (false);


parameterexpression Expression_param = Expression.parameter (Ttype, "P");


foreach (String key in dictionary. Keys)


43 {


Expression temp = expression.equal (Expression.call expression.property (Expression_param, Ttype.getproper Ty (key)), Ttype.getmethod ("ToString"),


expression.constant (Dictionary[key]);


Expression_return = expression.or (Expression_return, temp);


47}


(expression<func<t, bool>>) expression.lambda<func<t, bool>> (Expression_ Return, new parameterexpression[] {expression_param});


49}


50

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.