title: How do I query a local datatable and return information to a datatable in VB.net
body: <p>I am trying to pass a query and existing datatable into a function. The function will query the passed datatable using the passed query and return the result. </p>
  
  <p>Unfortunately, I am unable to return any data. I have posted my code below. Can anyone help me fix it? I don't know what I am doing wrong. </p>
  
  <pre><code>Public Function ExecQueryTest(Query As String, DT As DataTable) As DataTable
  
      Dim Result() As DataRow
      'initialize the table to have the same number of columns of the table that is passed into the function
      Dim LocalTable As DataTable = DT
      'initialize counting variables
      Dim x, y As Integer
  
      'use the select command to run a query and store the results in an array
      Result = DT.Select(Query)
  
     'remove all items from the localtable after initial formatting
      For x = 0 To LocalTable.Rows.Count - 1
          LocalTable.Rows.RemoveAt(0)
      Next
  
      'for loop to iterate for the amount of rows stored in result
      For x = 0 To Result.GetUpperBound(0)
          'add each array row into the table 
          LocalTable.Rows.Add(Result(x))
      Next
  
      ExecQueryTest = LocalTable
  End Function
  </code></pre>
  
  <p>If there is a better way to accomplish my goal, I don't mind starting from scratch. I just want to be able to handle dynamic tables, queries, and be able to return the information in a datatable format. </p>