zoukankan      html  css  js  c++  java
  • linq 实现查询字符串拼接 : And 和 OR 两种方式

    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();  
        }  
  • 相关阅读:
    每日总结
    每日总结
    《构建之法》读后感3
    每日博客
    每日博客
    每日博客
    每日博客
    预开发软件书
    每日博客
    每日博客
  • 原文地址:https://www.cnblogs.com/moxuanshang/p/5227429.html
Copyright © 2011-2022 走看看