我有一个DataTable
|------------|
| 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 |
|------------|
填充数据集和数据表的查询仍然让我感到困惑。谢谢你的帮助。
答案 0 :(得分:2)
您首先filter the data:
MyTable.DefaultView.RowFilter = "x = 1"
然后你copy the view to a new table:
Dim MyNewTable As DataTable = Mytable.DefaultView.ToTable
答案 1 :(得分:1)
您可以尝试创建新的DataTable并克隆原始DataTable以引入架构和约束。然后过滤行并将其添加到新的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