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,有什么办法达到这样的目的,求代码,
我,,linq菜来的啊。。
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();
}
}
}
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();
}
{
query = query.Where(x => x.Name == name);
}
if (sname != "")
query.where(data=>data.PlayerName=="'"+sname+"'")
if (grpname != "播放器组")
query.where(data=>data.GRPName=="'"+grpme+"'")