获得不同的值并转换回DataTable

时间:2012-03-14 16:59:37

标签: vb.net linq

我有一个“结果”,它是DataTable,我喜欢得到x个不同的值(City)并将其转换回DataTable我该怎么做这个下面的代码给我一个错误

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

所以我想要的是我可以得到的不同城市的记录,但我遇到的问题是将其转换回Datable。

编辑:

我在这个语句中使用“where”并且它确实转换为Table(工作正常)但不是“select”

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

1 个答案:

答案 0 :(得分: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

相关问题