Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有组合框和列的动态Linq到Sql。包含_C#_Database_Linq_Linq To Sql_Dynamic Sql - Fatal编程技术网

C# 带有组合框和列的动态Linq到Sql。包含

C# 带有组合框和列的动态Linq到Sql。包含,c#,database,linq,linq-to-sql,dynamic-sql,C#,Database,Linq,Linq To Sql,Dynamic Sql,我在表单上有一个文本框、组合框、按钮和DataGridView,用于从MSSQL视图vCustomer搜索和返回客户信息。它工作得很好,但我知道我的代码可以更有效。组合框中的四项表示要搜索的列 有没有一种简单的方法可以将以下内容转换为动态LINQ到SQL?我是C的新手。我查看了一些其他帖子,但我似乎无法让它工作 public partial class MainForm : Form { public MainForm() { InitializeComponen

我在表单上有一个文本框、组合框、按钮和DataGridView,用于从MSSQL视图vCustomer搜索和返回客户信息。它工作得很好,但我知道我的代码可以更有效。组合框中的四项表示要搜索的列

有没有一种简单的方法可以将以下内容转换为动态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;

嘿,罗尼。我尝试了你的建议,重新考虑了我的代码,见下文。但是,我收到一个错误:“vCustomer”类型中不存在属性或字段“smith”。顺便说一下,MessageBox.Showcondition;行返回Name.Containssmith,它看起来正确

我做错了什么?对不起,我是个笨蛋,谢谢你的帮助

我想出来了。。。需要用双引号包装搜索字符串!代码已被编辑

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; 
        }
    }
}

我在CSharpSamples文件中找到了一个名为Dynamic.cs的文件。此文件具有正确的命名空间-这是您的意思吗?我会试试的。哦,老兄。。。如果我在搜索字段中输入smith,它就会工作。所以,我想我只是在我的搜索字符串周围加上双引号,这对所有人都有效!我试过o'rei,成功了!我将更新我的代码。我将txtSearch.Text改为,\+txtSearch.Text+\并且它可以工作!注意,如果列的类型为Integer,则此操作不起作用。如果列的类型为Int,则必须删除双引号。是否有方法首先检查列的数据类型?