将特定行添加到新数据表

时间:2013-07-22 10:14:27

标签: .net vb.net

只想选择星期一10和星期二。

 Monday    Tuesday    Wed
 10           40       9
 20           50       6
 30           70       4

我到目前为止的代码:

    For Each row As DataRow In table.Rows

        table2.Rows.Add(row(0), row(1))

    Next

这增加了星期一和星期二的全部,但我只想要星期一的1个数据和星期二的1个数据。 ?

起初我以为我需要添加新行?

           For Each row As DataColumn In table.Columns
           table2.Rows.InsertAt(newRowb, 0)
           table2.Rows.Add(row*(0), row(1))

 ***getting mixed up with rows and columns, as am having errors on *

           Next

但是新行有错误,但有没有办法添加特定数据

1 个答案:

答案 0 :(得分:1)

您可以使用Linq-to-DataSet

Dim filteredRows = From row In table
                   Where  row.Field(Of Int32)("Monday") = 10 _
                   OrElse row.Field(Of Int32)("Tuesday") = 50
table2 = filteredRows.CopyToDataTable()

  

无论如何都是使用循环这样做,因为这是手动。使用一些   循环?

当然:

Dim table2 = table.Clone()
For Each row As DataRow In table.Rows
    Dim monCount = row.Field(Of Int32)("Monday")
    Dim tueCount = row.Field(Of Int32)("Tuesday")
    If monCount = 10 OrElse tueCount = 50 Then
        Dim newRow = table2.Rows.Add()
        newRow.ItemArray = row.ItemArray
    End If
Next