码迷,mamicode.com
首页 > 其他好文 > 详细

linq 实现查询字符串拼接 : And 和 OR 两种方式

时间:2016-02-29 16:20:21      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

MXS&Vincene  ─╄OvЁ  &0000019─╄OvЁ  MXS&Vincene 

MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳。

MXS&Vincene  ─╄OvЁ:We‘re here to put a dent in the universe. Otherwise why else even be here? 

 

正文>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

////linq  拼接字符串 and
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();  //上边的所有if,只有到此处才会执行  
       }  


////linq   拼接字符串 or
public void Test(string a,string b,string c,string d)  
    {  
             QueryContext query = new QueryContext();  
             var q1 = from u in query.Users  
                      where u.name== a && a != ""  
                  || u.age == b && b != ""  
                  || u.sex == c && c != ""  
                  || u.address ==d && d !=""   
                      select u;  
             q1.ToList();  
    }  

 

linq 实现查询字符串拼接 : And 和 OR 两种方式

标签:

原文地址:http://www.cnblogs.com/Vincene/p/5227429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!