动态的LINQ to SQL随着组合框和Column.Contains
-
20-08-2019 - |
题
我有用于搜索和从MSSQL视图(vCustomer)返回客户信息的表格上的文本框,组合框,按钮和DataGridView中。它的伟大工程,但我知道我的代码可以更有效。在组合框的四个项目表示要搜索的列。
是否有转换到以下动态LINQ到SQL的一个简单的方法?我是新来的C#。我查了一些其他职位,但我似乎无法得到它的工作。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// columns to filter for
string[] list = new string[4];
list[0] = "Name";
list[1] = "CustomerAccountNo";
list[2] = "Telephone";
list[3] = "Postal";
// bind to combobox
cboColumn.DataSource = list;
cboColumn.SelectedIndex = 0;
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
CustomerSearchDataContext db = new CustomerSearchDataContext();
IEnumerable<vCustomer> customerQuery = null;
switch (cboColumn.SelectedIndex)
{
case 0:
customerQuery = from c in db.vCustomers
where c.Name.Contains(txtSearch.Text)
orderby c.CustomerAccountNo descending
select c;
break;
case 1:
customerQuery = from c in db.vCustomers
where c.Name.Contains(txtSearch.Text)
orderby c.CustomerAccountNo descending
select c;
break;
case 2:
customerQuery = from c in db.vCustomers
where c.Telephone.Contains(txtSearch.Text)
orderby c.CustomerAccountNo descending
select c;
break;
case 3:
customerQuery = from c in db.vCustomers
where c.Postal.Contains(txtSearch.Text)
orderby c.CustomerAccountNo descending
select c;
break;
}
customerBindingSource.DataSource = customerQuery;
dataGridView1.DataSource = customerBindingSource;
dataGridView1.Columns["CustomerId"].Visible = false;
}
catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show("An Error Occured - " + ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
}
解决方案
使用[System.Linq.Dynamic][1]
。
从一个方法获取条件并在单个查询中使用它。
switch (choice)
{
case case1:
condition = string.Format("{0}.Contains({1})", "Column", "Value"
break;
其他提示
嘿罗尼。我想你的建议,并重新分解我的代码(见下文)。然而,我收到错误:否属性或字段“SMITH”型“vCustomer” 强>存在。顺便说,该MessageBox.Show(条件);线返回的 Name.Contains(史密斯)强>这看起来是正确的。
我在做什么错了?对不起,是你的帮助一个noob和感谢。
想通了......包的搜索字符串用双引号需要!代码已被编辑。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// data column to filter against
string[] list = new string[4];
list[0] = "Name";
list[1] = "CustomerAccountNo";
list[2] = "Telephone";
list[3] = "Postal";
cboColumn.DataSource = list;
cboColumn.SelectedIndex = 0;
// left, right or middle search
string[] list2 = new string[3];
list2[0] = "Contains";
list2[1] = "StartsWith";
list2[2] = "EndsWith";
cboFilterAtt.DataSource = list2;
cboFilterAtt.SelectedIndex = 0;
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
CustomerSearchDataContext db = new CustomerSearchDataContext();
//string condition = string.Format("{0}.{1}({2})", cboColumn.SelectedValue, cboFilterAtt.SelectedValue, txtSearch.Text);
string condition = string.Format("{0}.{1}({2})", cboColumn.SelectedValue, cboFilterAtt.SelectedValue, "\"" + txtSearch.Text + "\"");
MessageBox.Show(condition);
var customerQuery = db.vCustomers.Where(condition).OrderBy("CustomerAccountNo");
customerBindingSource.DataSource = customerQuery;
dataGridView1.DataSource = customerBindingSource;
dataGridView1.Columns["CustomerId"].Visible = false;
}
catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show("An Error Occured - " + ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor.Current = Cursors.Default;
}
}
}
不隶属于 StackOverflow