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