Code
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();
}
原文地址:http://www.cnblogs.com/wangshijie/archive/2009/05/09/1453340.html
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();
}