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)