string sname=context.Request["Name"].ToString();
                       string grpname=context.Request["GrpName"].ToString();
                       string state=context.Request["State"].ToString();
                       string province=context.Request["Province"].ToString();
                       string city=context.Request["City"].ToString();
                       string location=context.Request["Location"].ToString();
                       string template=context.Request["Template"].ToString();
                       string playtype=context.Request["PlayType"].ToString();/*这是从前台发到ashx文件的查询条件,但是又一些是空值,
于是想做个判断然后在决定要不要给查询语句加上该条件:例如(我用ADO.net的方式是这样拼接进去的):*/
         string strsql = "select * from Player_Inf_t where ";
         if (sname != "")strsql += " PlayerName='"+sname+"' and ";
         if (grpname != "播放器组") strsql +="GrpName='"+grpname+"' and ";
         if(state!="连接状态")strsql+="WorkState="+(state=="未连接"?0:1)+" and ";
         if (province != "省份") strsql+="Province ='"+province+"' and ";
         if (city != "市县"||city!="")strsql+="City='"+city+"' and ";
         if (location != "乡镇" || location != "")strsql+="Place='"+location+"' and ";
         if (template != "播放器模板") strsql+="TemplateName='"+template+"' and ";
         if (playtype != "播放器类型") strsql += "Type_='"+playtype+"' and ";
         strsql += "1=1";       //如果想换做linq,有什么办法达到这样的目的,求代码,
                                

解决方案 »

  1.   

    lamda表达式构造expression作为where的条件
      

  2.   

    那你们给个代码示例看一下啊。。
    我,,linq菜来的啊。。
      

  3.   

    linq来拼接查询语句,貌似有点难,我也是linq新手,路过学习
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq;namespace LINQ
    {
          public  class LinqTosql
        
        {
         private string connstring = "server=.;database=Student;integrated security=true;";
           /// <summary>
           /// 添加
           /// </summary>
         public void insert()    
         {
             DataContext dt = new DataContext(connstring);
             Table<student> table = dt.GetTable<student>();         student stu = new student
             {
                 ID="1004",
                 name="lili"         };         table.InsertOnSubmit(stu);
             dt.SubmitChanges();
         }
          /// <summary>
          /// 查询
          /// </summary>     public void Select()
         {
             DataContext dt = new DataContext(connstring);
             Table<student> table=dt.GetTable<student>();
             var linq =
                 from stu in table
                 select stu;         foreach(var st in linq)
             {
                 Console.WriteLine(st.ID+":"+st.name);
             }
         }
            /// <summary>
            /// 修改
            /// </summary>
         public void Update()
         {
             DataContext dt = new DataContext(connstring);
             Table <student> table= dt.GetTable<student>();
             var linq =
                 from stu in table
                 where stu.ID == "1004"
                 select stu;
             foreach(var stu in linq)
             {
                 stu.name = "更新";
             }
             dt.SubmitChanges();
         }
              /// <summary>
              /// 删除
              /// </summary>
         public void Delete()
         {
             DataContext dt = new DataContext(connstring);
             Table<student> table = dt.GetTable<student>();
             var linq =
                 from stu in table
                 where stu.ID=="1004"
                 select stu;
             foreach(var st in linq)
             {
                 table.DeleteOnSubmit(st);
             }
             dt.SubmitChanges();
         }
              /// <summary>
              /// 自定义dt
              /// </summary>
         public void select()
         {
             StudentDataContext sc = new StudentDataContext(connstring);
             var linq =
                 from tea in sc.teachers
                 join cl in sc.classes
                 on tea.tid equals cl.cid
                 select new { teacher = tea.tid, clstitle = cl.ctitle};
             foreach(var c in linq)
             {
                 Console.WriteLine(c.teacher+":"+c.clstitle);
             }         Console.ReadKey();
           
         }
        }
    }
      

  5.   

    ……楼上的楼上,额,写很多代码的那位兄弟。。很感谢你有认真写了很多代码,但是,你可以先弄明白我的问题嘛。。我知道  var aa=from u in db.UserInf_t where id=="xxx" select u但是现在的问题是where语句里面究竟需要几个限制条件不是我能决定的好吧,是根据传过来的值是否=="" 或者xxx 决定要不要添加某项限制
      

  6.   

    构造,多个可以用数组,expression的and运算我老是调不通……能用expression的and最好
      Expression<Func<Op, bool>> expAppIndex = n => n.App.AppIndex == AppIndex;
    使用
     protected int Count<T>(params Expression<Func<T, bool>>[] where) where T : class
            {
                var query = CurrentSession.Query<T>();
                if (where != null)
                {
                    foreach (Expression<Func<T, bool>> e in where)
                    {
                        query = query.Where(e);
                    }
                }
                return query.Cacheable().Count();
            }
      

  7.   

    var query = provider.Query<Player>();if(!String.IsNullOrEmpty(name))
    {
        query = query.Where(x => x.Name == name);
    }
      

  8.   

    都是and的话 可以直接这样 var query = from data in Player_Inf_t select data;
             if (sname != "")
                query.where(data=>data.PlayerName=="'"+sname+"'")
             if (grpname != "播放器组") 
                 query.where(data=>data.GRPName=="'"+grpme+"'")
      

  9.   

    额,感谢大家得帮助,。。有些高深,也很优雅,呵呵,我得好好琢磨琢磨,就按照原来我那写法,加个语句就可以实现了。。搓劣了一些,呵呵var products = db.ExecuteQuery<QMPlayerInfModel>(strsql);//QMPlayerInfModel是与表相同结构的一个model类,
      

  10.   

    发个国外牛人写的 PredicateBuilder 扩展这个就很灵活了
      

  11.   

    http://www.albahari.com/nutshell/predicatebuilder.aspx