日期:2014-05-18 浏览次数:21132 次
StringBuilder sb = new StringBuilder();
sb.Append(" select * from feemain where 1=1");
if (cmb_sf.Text != "")
sb.Append(" and wtdw='" + cmb_sf.Text + "'");
if (txt_blno.Text != "")
sb.Append(" and blno='" + txt_blno.Text + "'");
if (usetime.Checked)
sb.Append(" and indatatime>='" + dtp1.Value + "' and indatatime<='" + dtp2.Value + "'");
if (cmb_ywlx.Text != "")
sb.Append(" and receive='" + cmb_ywlx.Text + "'");
DataSet dd = Maticsoft.DBUtility.DbHelperSQL.Query(sb.ToString());
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dd.Tables[0];
//上面的是一般的的查询的方法
//如果换成linq的写法的话 怎样将 这个 StringBuilder 也就是sb的值写进linq语句呢?
//求帮助 下面不会写了 因为这个查询条件是不断的变化的 所以只能用这个动态的增加查询条件
StartLinqDataContext con = new StartLinqDataContext();
var q=from c in con.FeeMain where ???sb.ToString()??? select c;//这儿怎么写呢???
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = q;
StartLinqDataContext con = new StartLinqDataContext();
var q=con.FeeMain;
if (cmb_sf.Text != "")
q = q.Where(w => w.wtdw == cmb_sf.Text);
if (txt_blno.Text != "")
q = q.Where(w => w.blno == txt_blno.Text);
if (usetime.Checked)
q = q.Where(w => w.indatatime>= Convert.ToDateTime(dtp1.Value)&&w.indatatime<= Convert.ToDateTime(dtp2.Value));
if (cmb_ywlx.Text != "")
q = q.Where(w => w.receive == cmb_ywlx.Text);
------解决方案--------------------
StartLinqDataContext con = new StartLinqDataContext();
var q=from c in con.FeeMain
select c;
if (cmb_sf.Text != "")
{q = from h in q
where h.wtdw == cmb_sf.Text
select h;}
if (txt_blno.Text != "")
{q = from h in q
where h.blno == txt_blno.Text
select h;}
if (usetime.Checked)
{q = from h in q
where h.indatatime >= Convert.ToDateTime(dtp1.Value)&& h.indatatime<= Convert.ToDateTime(dtp2.Value));
select h;}
if (cmb_ywlx.Text != "")
{q = from h in q
where h.receive == cmb_ywlx.Text
select h;}
------解决方案--------------------
楼主能分享最终的解决办法,赞一个。对后来遇到此问题的人是一种帮助:)
你也可以参考这个:
http://blog.csdn.net/q107770540/article/details/5724013
------解决方案--------------------
LINQ的延迟查询特性决定了,只有需要用到结果中的数据时,程序才会去执行查询
//所以下边的where拼接有多少个,都只会在最后一步时才会去执行查询
var qu = from c in con.FeeMain select c;
if (textBox1.Text != "")
qu = qu.Where(c => c.wtdw == textBox1.Text