2017-05-14 6 views
0

私は "UniversalClientSearch"と呼ばれる検索メソッドを持ち、データハンドラクラスにあります。メソッドは、どの列とその列で検索するかに応じてテーブル内のデータを検索し、そのメソッドは、自分のフォームのデータグリッドビューで使用するテーブル変数を返す必要があります。LinQ to SQLを使用してテーブルを返す<tablename>

LinQからSQLステートメントを作成して、必要なデータの行を取得し、var ReturnSearchQueryに保存しましたが、そのクエリをTableの変数に変換する方法がわかりません。 tablenameはtbclientです。あなたのコード内

 //Fields 
     private DataAccessDataContext db; 

     //Constructor 
     public Datahandler() 
     { 
      this.db = new DataAccessDataContext(); 
     } 

     //Method 
     public Table<tbClient> UniversalClientSearch(string SearchType, string SearchParameter) 
     { 
      //This is just here to initialize the variable so it can be changed in the switch 
      var ReturnSearchQuery = from Clients in db.tbClients 
            select Clients; 

      //Filters Query based on what column to search 
      switch (SearchType) 
      { 
       case "Client_ID": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Client_ID == int.Parse(SearchParameter) 
         select Clients; 
        break; 

       case "Client_Name": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Client_Name.Contains(SearchParameter) 
         select Clients; 
        break; 

       case "Client_Address": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Client_Address.Contains(SearchParameter) 
         select Clients; 
        break; 

       case "Contact_Number": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Contact_Number.Contains(SearchParameter) 
         select Clients; 
        break; 

       case "Contact_Email": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Contact_Email.Contains(SearchParameter) 
         select Clients; 
        break; 

       case "Client_Type": 
        ReturnSearchQuery = from Clients in db.tbClients 
         where Clients.Client_Type == SearchParameter 
         select Clients; 
        break; 
      } 

      //Make new table to be returned later 
      Table<tbClient> ReturnClientsTable = new Table<tbClient>(); 

      foreach (var item in ReturnSearchQuery) 
      { 
       //Query has to be converted to Table<tbClient> 
      } 

      //Table return to be used in datagridview 
      return ReturnClientsTable; 
     } 

答えて

1

ReturnSearchQueryタイプIQueryable<tbClient>あります

// The SQL query isn't run yet 
IQueryable<tbClient> ReturnSearchQuery = from Clients in db.tbClients 
      where <some condition> 
      select Clients; 

ため、遅延実行のをあなたはIQueryableを列挙するまで、SQLクエリが実行されません。

// Convert the IQueryable to a list. Here, the SQL query is run. 
List<tbClient> clients = ReturnSearchQuery.ToList(); 

最後に、あなたは.DataSourceを使用してDataGridViewにクライアントのリストをバインドすることができます。

var d = new DataGridView(); 
d.DataSource = clients; 
+0

おかげで、私は自分のプロジェクトにこれを組み込みますよ! **私は答えを得ることができませんでしたが、** UniversalClientSearch **メソッドの戻り値の型をdynamicに変更し、** ReturnSearchQuery **を返すだけでした。 –

関連する問題