网址参数是url.aspx?A=123&B=x|y|z&C=12|34&D=u|v|w
当然有可能ABCD几个参数也可能是空的,以上是全部满的状况
linq to sql如何进行多条件查询?linq to sql多条件

解决方案 »

  1.   

     N年前我们是这样来 拼接查询字符串的:  
    [c-sharp] view plaincopy
    01.public string Test(string a, string b, string c,string d)  
    02.   {  
    03.       string sql = "SELECT * FROM Users WHERE 1=1";  
    04.       if (!string.IsNullOrEmpty(a))  
    05.       {  
    06.           sql += " AND name='" + a + "'";  
    07.       }  
    08.       if (!string.IsNullOrEmpty(b))  
    09.       {  
    10.           sql += " AND age='" + b+ "'";  
    11.       }  
    12.       if (!string.IsNullOrEmpty(c))  
    13.       {  
    14.           sql += " AND sex='" + c + "'";  
    15.       }  
    16.       if (!string.IsNullOrEmpty(d))  
    17.       {  
    18.           sql += " AND address='" + d + "'";  
    19.       }  
    20.       return sql.ToString();  
    21.   }  
      现在我们使用linq来实现上边的代码: 
    [c-sharp] view plaincopy
    01.public void Test(string a, string b, string c,string d)  
    02.       {  
    03.           QueryContext query = new QueryContext();  
    04.           var q = from u in query.Users  
    05.                    select u;  
    06.           if (!string.IsNullOrEmpty(a))  
    07.           {  
    08.               q = q.Where(p => p.name == a);  
    09.           }  
    10.           if (!string.IsNullOrEmpty(b))  
    11.           {  
    12.               q = q.Where(p => p.age == b);  
    13.           }  
    14.           if (!string.IsNullOrEmpty(c))  
    15.           {  
    16.               q = q.Where(p => p.sex == c);  
    17.           }  
    18.           if (!string.IsNullOrEmpty(d))  
    19.           {  
    20.               q = q.Where(p => p.address == d);  
    21.           }  
    22.           q.ToList();  //上边的所有if,只有到此处才会执行  
    23.       }  
      

  2.   

    http://blog.csdn.net/q107770540/article/details/5724013
      

  3.   

    public string Test(string a, string b, string c,string d)  
    02.   {  
    03.       string sql = "SELECT * FROM Users WHERE 1=1";  
    04.       if (!string.IsNullOrEmpty(a))  
    05.       {  
    06.           sql += " AND name='" + a + "'";  
    07.       }  
    08.       if (!string.IsNullOrEmpty(b))  
    09.       {  
    10.           sql += " AND age='" + b+ "'";  
    11.       }  
    12.       if (!string.IsNullOrEmpty(c))  
    13.       {  
    14.           sql += " AND sex='" + c + "'";  
    15.       }  
    16.       if (!string.IsNullOrEmpty(d))  
    17.       {  
    18.           sql += " AND address='" + d + "'";  
    19.       }  
    20.       return sql.ToString();  
    21.   }  
      这样写可是要写属性的哦