2009-06-25 21 views
1

MSSQLビュー(vCustomer)から顧客情報を検索して返すためのフォームに、テキストボックス、コンボボックス、ボタン、およびDataGridViewがあります。それは素晴らしい作品ですが、私のコードがより効率的であることがわかります。コンボボックスの4つの項目は、検索する列を表します。動的Linq to ComboBoxとColumn.Containsを持つ

以下を動的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; 
     } 
    } 
} 

答えて

2

[System.Linq.Dynamic][1]を使用してください。

メソッドから条件を取得し、単一のクエリで使用します。

switch (choice) 
    { 
     case case1: 
      condition = string.Format("{0}.Contains({1})", "Column", "Value" 
      break; 
+0

CSharpSamplesファイル内にDynamic.csというファイルがあります。このファイルには適切な名前空間があります。これは意味ですか?私はそれを試してみる。 – robnardo

+0

はいDynamic.csにダイナミックLinqライブラリがあります – Rony

0

Hey Rony。私はあなたの提案を試み、自分のコードを組み込んだ(下記参照)。しかし、私はエラーを受け取ります:'vCustomer'タイプにプロパティまたはフィールド 'smith'が存在しません。ところで、MessageBox.Show(条件);行は名前を返します。正確に見える(smith)

私は間違っていますか?あなたのお手伝いをさせていただきありがとうございます。

二重引用符で検索文字列を囲む必要があります。コードが編集されました。

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

Oh man ...検索フィールドに「smith」と入力すると動作します。だから、私はちょうど私の検索文字列の周りに二重引用符を追加すると私はすべてのために動作すると思う!私は "オレイ"を試してみました!コードを更新します。私はtxtSearch.Textを "\" "+ txtSearch.Text +" \ ""に変更しました。 – robnardo

+0

列がInteger型である場合、これは機能しません。列がInt型の場合は、二重引用符を削除する必要があります。列のデータ型が最初に何であるかを確認する方法はありますか? – robnardo

関連する問題