Predicatebuilder class)

Source: Internet
Author: User
The predicatebuilder class is as follows:
Public static class predicatebuilder {// <summary> // when the function is applied to true: one and is valid, multiple and are valid; one or is invalid, and multiple or is invalid; the or statement written after "and" is valid. // </Summary> // <typeparam name = "T"> </typeparam> // <returns> </returns> public static expression <func <t, bool> true <t> () {return F => true ;}/// <summary> // when the function is set to false: A single and is invalid, multiple and values are invalid. One or more or values are valid; valid and after or when mixing: // </Summary> // <typeparam name = "T"> </typeparam> // <returns> </returns> public static expression <func <t, bool> false <t> () {return F => false;} public static expression <func <t, bool> or <t> (this expression <func <t, bool> expr1, expression <func <t, bool> expr2) {var invokedexpr = expression. invoke (expr2, expr1.parameters. cast <expression> (); Return expression. lambda <func <t, bool> (expression. or (expr1.body, invokedexpr), expr1.parameters);} public static expression <func <t, bool> and <t> (this expression <func <t, bool> expr1, expression <func <t, bool> expr2) {var invokedexpr = expression. invoke (expr2, expr1.parameters. cast <expression> (); Return expression. lambda <func <t, bool> (expression. and (expr1.body, invokedexpr), expr1.parameters );}}

Code for multi-condition query:

/// <Summary> /// multi-condition query /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> protected void btnsearch_click (Object sender, eventargs e) {using (linqdbdatacontext DB = new linqdbdatacontext () {var list = dB. stuinfo; var where = predicatebuilder. true <stuinfo> (); if(this.txt name. text. trim (). length! = 0) {where = where. And (P => p.stuname.contains(this.txt name. Text. Trim ();} if(this.txt age. Text. Trim (). length! = 0) {where = where. and (P => P. stuage = convert.toint32(this.txt age. text. trim ();} var result = List. where (where ). tolist (); this. repstuinfo. datasource = result; this. repstuinfo. databind ();}}

In the code above, txtname is the name text box, And txtage is the age text box. Because the and condition query is required, predicatebuilder is used at the beginning. true <stuinfo> () to create the where condition whose initial value is true,

If multi-condition or query is performed, use predicatebuilder. False <stuinfo> () to create the where condition with the initial value of false.

Predicatebuilder class)

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.