去看更多

wangshijie
Welcome

用Linq 实现动态多条件查询

private void ViewBinding()
        {    
            Expression<Func<TestTable, bool>> expr = n => GetCondition(n);
            var xQuery = DCDataContext.TestTables.Where<TestTable>(expr.Compile());
            this.dataGridView1.DataSource = xQuery.ToList<TestTable>();
        }
        private bool GetCondition(TestTable tb)
        {
            bool boolResult = true;
            if (txtUserNumber.Text.Trim() != string.Empty)
            {
                boolResult &= tb.UserNumber == int.Parse(txtUserNumber.Text.Trim());
            }
            if (txtName.Text.Trim() != string.Empty)
            {
                boolResult &= tb.Name == txtName.Text.Trim();
            }
            if (txtClassName.Text.Trim() != string.Empty)
            {
                boolResult &= tb.ClassName == txtClassName.Text.Trim();
            }
            return boolResult;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ViewBinding();
        }
posted @ 2009-05-09 19:40  jeer  阅读(468)  评论(1编辑  收藏  举报