2012-03-14 30 views
1

我有一個「結果」,它是DataTable,我喜歡獲得x個不同的值(城市)並將其轉換回DataTable,我該如何做到這一點下面的代碼給了我一個錯誤獲取不同的值並將其轉換回DataTable

Dim query = (From x In results.AsEnumerable() 
         Select (x.Field(Of String)("City"))).Distinct().CopyToDataTable() 

所以我想要的是我可以得到的不同城市的記錄,但是我遇到的問題是將其轉換回Datable。

編輯:

我「其中」在這個語句中使用它並轉換爲表(正常工作),但不是「選擇」

Dim results = (From myRow In ds.Tables(1).AsEnumerable() 
      Where (myRow.Field(Of String)("xxxx") = xxxx) 
          Select myRow).Distinct().CopyToDataTable() 

回答

0

這個你正在尋找一個例子嗎?

' Bind the System.Windows.Forms.DataGridView object 
' to the System.Windows.Forms.BindingSource object. 
dataGridView.DataSource = bindingSource 

' Fill the DataSet. 
Dim ds As New DataSet() 
ds.Locale = CultureInfo.InvariantCulture 
' See the FillDataSet method in the Loading Data Into a DataSet topic. 
FillDataSet(ds) 

Dim orders As DataTable = ds.Tables("SalesOrderHeader") 

' Query the SalesOrderHeader table for orders placed 
' after August 8, 2001. 
Dim query = _ 
    From order In orders.AsEnumerable() _ 
    Where order.Field(Of DateTime)("OrderDate") > New DateTime(2001, 8, 1) _ 
    Select order 

' Create a table from the query. 
Dim boundTable As DataTable = query.CopyToDataTable() 

' Bind the table to a System.Windows.Forms.BindingSource object, 
' which acts as a proxy for a System.Windows.Forms.DataGridView object. 
bindingSource.DataSource = boundTable 

http://msdn.microsoft.com/en-us/library/bb386921.aspx

相關問題