[Conclusion] dynamic Lambda expression Construction

Source: Internet
Author: User

 

Go directlyCode:

 VaR Ints = New   Int [] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9  }; //  VaR r = ints. Where (I => I> 5 );  //  Expression to be implemented  //  Create parameter I              VaR Parameter = Expression. parameter (  Typeof ( Int ), "  I  "  );  // Create constant 5              VaR Constant = Expression. Constant (  5  );  //  Create a comparison expression I> 5              VaR Bin = Expression. Equal (parameter, constant); Bin = Expression. Or (bin, expression. Equal (parameter, expression. Constant ( 3  )));  //  Obtain lambda expressions             VaR Lambda = Expression. Lambda <Func < Int , Bool > (Bin, parameter );  //  Get the delegate using the compile Method              VaR _ R = Ints. Where (lambda. Compile ());  Foreach ( VaR V In  _ R) response. Write (V + "  <Br>  " );

 

 

Build a dynamic Lambda expression for a custom object:

 

 Public   Class  Student {  Public   Int Id { Get ; Set  ;}  Public   String Name {Get ; Set  ;}}  //  Build Test Data  List <Student> students = New List <student> (); Students. Add (  New Student () {id = 1 , Name = "  Zhang San  "  }); Students. Add (  New Student () {id = 2 , Name = "  Li Si  "  }); Students. Add (  New Student () {id = 3 , Name = "  Wang 'er  "  }); Students. Add (  New Student () {id = 4 , Name = "  Li Xiaolan "  }); Students. Add (  New Student () {id = 5 , Name = "  Huang Wu  "  });  //  Construct a parameter expression  VaR Parameter = expression. parameter ( Typeof (Student ), "  Student  " );  //  Construct an object attribute expression  Memberexpression = Expression. Property (parameter, "  ID  "  );  //  Build constant expressions  VaR Constant = expression. Constant ( 1  );  //  Expression combination effect equals to student => Student. ID = 1 VaR Bin = Expression. Equal (memberexpression, constant );  //  The expression combination effect is equal to student => Student. ID = 1 & Student. ID = 2  Bin = Expression. Or (bin, expression. Equal (memberexpression, expression. Constant ( 2  )));  //  Obtain lambda expressions              VaR Lambda = Expression. Lambda <Func <student, Bool >(Bin, parameter );  VaR _ R = students. Where <student> (Lambda. Compile ());  Foreach (Student s In  _ R) {response. Write (S. Name + "  <Br>  "  );} 

 

Display result:

Zhang San
Li Si

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.