Concatenate query strings using the "And" And "OR" method.

Source: Internet
Author: User

Concatenate query strings using the "And" And "OR" method.

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

// He asked hovertree. compublic string Test (string a, string B, string c, string d) {string SQL = "SELECT * FROM Users WHERE 1 = 1"; if (! String. IsNullOrEmpty (a) {SQL + = "AND name = '" + a + "'";} if (! String. IsNullOrEmpty (B) {SQL + = "AND age = '" + B + "'";} if (! String. IsNullOrEmpty (c) {SQL + = "AND sex = '" + c + "'";} if (! String. IsNullOrEmpty (d) {SQL + = "AND address = '" + d + "'" ;}return SQL. ToString ();}

Now we use linq to implement the above Code:

Public void Test (string a, string B, string c, string d) {QueryContext query = new QueryContext (); var q = from u in query. Users select u; if (! String. IsNullOrEmpty (a) {q = q. Where (p => p. name = a);} if (! String. IsNullOrEmpty (B) {q = q. Where (p => p. age = B);} if (! String. IsNullOrEmpty (c) {q = q. Where (p => p. sex = c);} if (! String. isNullOrEmpty (d) {q = q. where (p => p. address = d);} q. toList (); // all the above if will be executed only here} // What is hovertree.com?

Recommended: http://www.cnblogs.com/roucheng/p/dushubiji.html

Related Article

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.