c# - 使用 ComboBox 和 Column.Contains 的动态 Linq To Sql

标签 c# database linq linq-to-sql dynamic-sql

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

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

关于c# - 使用 ComboBox 和 Column.Contains 的动态 Linq To Sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1045146/

相关文章:

sql - 在 MySQL 中创建/写入权限

c# - 避免在 LINQ 查询中双重控制搜索

database - 甲骨文接口(interface)

php - 如何将在php中创建的变量传递给mysql数据库?

c# - 防止富文本框中的图片

c# - 为 json.net 编写可重用的 JsonConverter

c# - Linq for rank() 在 SQL Server 中等效 -

c# - 有没有办法简化Linq新对象初始化的初始化?

c# - 如何建立一个算法来找到一个组合,哪个总和最接近一个数字,它的差异在c#中的一个范围内

c# - 如何在IIS服务器上运行简单的服务器进程