【问题标题】:VB.NET Query DatatableVB.NET 查询数据表
【发布时间】:2012-01-06 00:41:48
【问题描述】:

我有一个数据表

|------------|
| id | x | y |
|------------|
|  1 | 1 | 1 |
|  2 | 1 | 2 |
|  3 | 2 | 1 |
|  4 | 2 | 2 |
|------------|

我想通过 x 的值过滤这个 DataTable 来生成一个新的 DataTable

if x = 1
|------------|
| id | x | y |
|------------|
|  1 | 1 | 1 |
|  2 | 1 | 2 |
|------------|

or x = 2 
|------------|
| id | x | y |
|------------|
|  3 | 2 | 1 |
|  4 | 2 | 2 |
|------------|

填充数据集和数据表的查询仍然让我感到困惑。感谢您的帮助。

【问题讨论】:

标签: vb.net datatable


【解决方案1】:

你先filter the data

MyTable.DefaultView.RowFilter = "x = 1"

那你copy the view to a new table:

Dim MyNewTable As DataTable = Mytable.DefaultView.ToTable

【讨论】:

    【解决方案2】:

    您可以尝试创建一个新的数据表并克隆原始数据表以引入架构和约束。然后过滤行并将其添加到新的 DataTable 中。

    Dim newDT As DataTable = oldDT.Clone()
    
    Dim filter As string = "x = 1"; 
    
    //get the rows from the that have been filtered
    DataRow[] filteredRows = oldDT.Select(filter);
    
    //add the rows to the new datatable
    For Each dr As DataRow In filteredRows
        newDT.ImportRow(dr)
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 2022-07-05
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多