Concatenate query strings using the "and" or "method.

Source: Internet
Author: User


N years ago, we joined the query string in this way:

 

 

Public String test (string a, string B, string C, string d) <br/>{< br/> string SQL = "select * from users where 1 = 1 "; <br/> If (! String. isnullorempty (A) <br/>{< br/> SQL + = "and name = '" + A + "'"; <br/>}< br/> If (! String. isnullorempty (B) <br/>{< br/> SQL + = "and age = '" + B + "'"; <br/>}< br/> If (! String. isnullorempty (c) <br/>{< br/> SQL + = "and sex = '" + C + "'"; <br/>}< br/> If (! String. isnullorempty (D) <br/>{< br/> SQL + = "and address = '" + D + "'"; <br/>}< br/> return SQL. tostring (); <br/>} 

 

Now we use LINQ to implement the above Code:

 

Public void test (string a, string B, string C, string d) <br/>{< br/> querycontext query = new querycontext (); <br/> var q = from u in query. users <br/> select U; <br/> If (! String. isnullorempty (A) <br/>{< br/> q = Q. where (P => P. name = A); <br/>}< br/> If (! String. isnullorempty (B) <br/>{< br/> q = Q. where (P => P. age = B); <br/>}< br/> If (! String. isnullorempty (c) <br/>{< br/> q = Q. where (P => P. sex = C); <br/>}< br/> If (! String. isnullorempty (D) <br/>{< br/> q = Q. where (P => P. address = D); <br/>}< br/> q. tolist (); // all the above if statements are executed only here <br/>} 

 

 

 

 

 

 

 

 

 

PS: If the query string is not "and", but "or ",

For example:

 

 Public String test (string a, string B, string C, string d) <br/>{< br/> string SQL = "select * from users where 1 = 1 "; <br/> If (! String. isnullorempty (A) <br/>{< br/> SQL + = "or name = '" + A + "'"; <br/>}< br/> If (! String. isnullorempty (B) <br/>{< br/> SQL + = "or age = '" + B + "'"; <br/>}< br/> If (! String. isnullorempty (c) <br/>{< br/> SQL + = "or sex = '" + C + "'"; <br/>}< br/> If (! String. isnullorempty (D) <br/>{< br/> SQL + = "or address = '" + D + "'"; <br/>}< br/> return SQL. tostring (); <br/>} 

 

 

In this case, you can implement the following through LINQ:

 

 

Public void test (string a, string B, string C, string d) <br/>{< br/> querycontext query = new querycontext (); <br/> var Q1 = from u in query. users <br/> where u. name = A &! = "" <Br/> | U. Age = B & B! = "" <Br/> | U. Sex = C & C! = "" <Br/> | U. Address = D & D! = "" <Br/> select U; <br/> q1.tolist (); <br/>} 

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.