最近做LINQ的一个测试工作,需要把多表的SQL动态条件查询转为LINQ的查询.
string strSQL="SELECT A.ORDER,B.ORDITM,A.ORDDAT,A.ORDTIM,B.PRICE FROM A JOIN B ON A.ORDER=B.ORDER JOIN C ON B.PN=C.PN ";
if(chkOrderDate.Checked == true)
{
strSQL+=" A.ORDDAT BETWEEN '"+fromDate.Text.Trim()+"' AND ''"+toDate.Text.Trim()+" ";
}
if(chkOrderNumber.Checked==true)
{
strSQL+=" A.ORDER='"+txtOrderNumber.Text.Trim()+"' ";
}
....送给DB执行.
请教如何转为LINQ动态查询.
现在已经建立LINQ To SQL 的datacontext
LQ_DBDataContext LQ_DB = new LQ_DBDataContext(ConnectionString);
varPO=from a in LQ_DB.A
from b in LQ_DB.B on A.ORDER equals B.ORDER
from c in LQ_DB.C on B.PN equals C.PN
where A.ORDER==txtOrderNumber.Text.Trim() && A.ORDDAT>=Convert.ToInt32(fromDate.Text.Trim()) && A.ORDDAT<=Convert.ToInt32(toDate.Text.Trim())
select new {
A.ORDER,
B.ORDITM,
A.ORDDAT,
A.ORDTIM,
B.PRICE
};
//WHERE条件是不对的.应该是实际选择的条件, USER选择的条件不一样, LINQ中的WHERE中的条件应该不一样, 会得出来不同的处理结果. 请教圈子中的高人如何写这样的多表变量条件查询动态LINQ ?
一只斗牛犬
摇曳的蔷薇
慕桂英3389331
繁星淼淼