网址参数是url.aspx?A=123&B=x|y|z&C=12|34&D=u|v|w
当然有可能ABCD几个参数也可能是空的,以上是全部满的状况
linq to sql如何进行多条件查询?linq to sql多条件
当然有可能ABCD几个参数也可能是空的,以上是全部满的状况
linq to sql如何进行多条件查询?linq to sql多条件
解决方案 »
- 求解,DataContract和Controller
- 如何实现对 System.Windows.Forms.DataGridView的增加、修改、上除操作
- help me
- 请教一个数据库设计问题,如何设计这样的表?
- 用WebRequest,WebResponse进行网页源代码下载的相关问题
- 怎样在进行了一些操作之后,让指定的页面自动刷新
- 大家谁用过dotnetbar4.8
- win form 绑定到具有多个表的数据的 DataGrid 如何判断进入DataGrid 控件中的子表
- 为何我的程序登录验证时不能区分大小写
- 关于WWF的一个问题,怀疑是一个严重的BUG
- C# + WebBrowser如何实现淘宝页面的自动登录
- C#与游戏
[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. }
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. }
这样写可是要写属性的哦