[转]LinqToSql的问题 多条件 动态变化查询条件怎样查询

//第一种方法
            DataClasses3DataContext con = new DataClasses3DataContext();
             
            var qu = from c in con.FeeMain select c;
             
            if (textBox1.Text != "")
                qu = qu.Where(c => c.wtdw == textBox1.Text);
            if (textBox2.Text != "")
                qu = qu.Where(c => c.vessel == textBox2.Text);
 
            dataGridView1.DataSource = qu;
//第二种方法
            DataClasses3DataContext con = new DataClasses3DataContext();
            var searchPredicate1 = PredicateExtensions.True<FeeMain>();
            if (textBox1.Text != "")
                searchPredicate1 = searchPredicate1.And(c => c.wtdw == textBox1.Text);
            if (textBox2.Text != "")
                searchPredicate1 = searchPredicate1.And(c => c.vessel == textBox2.Text);
 
            var a = from c in con.FeeMain.Where(searchPredicate1) select c;
            dataGridView1.DataSource = a;

 

posted @ 2013-01-22 10:31  louiskoo  阅读(399)  评论(0编辑  收藏  举报