DataView过滤并返回一个字符串数组

时间:2016-06-17 06:22:22

标签: c# vb.net

我有一个DataTable,我在DataTable上应用了一个过滤器

我有两个问题:

1)我想应用额外的过滤器,以便在应用过滤器后仅返回前10行。

2)我想在应用过滤器后将DataView转换为字符串数组

这是我到目前为止所做的:

我将前缀变量作为参数传递给下面的函数:

dt = GetNumbers()
    ' filtering in datatabase



    Dim dv As DataView = New DataView(dt)
    dv.RowFilter = "number like '" + prefix + "%'"

     ' I want to return only top 10 rows after the above filter is applied
     ' after this I want to convert the dataview to string array and return that.

    Return strArray

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这样的事情:

dv.ToTable().AsEnumerable().Take(10).Select(x => x.Field<string>("ColumnName")).ToArray();

答案 1 :(得分:0)

Function Top10(dt As DataTable, prefix As String) As String()
  Dim dv As New DataView(dt, "number LIKE '" & prefix & "%'", "number ASC", DataViewRowState.CurrentRows)
  Dim strArray(9) As String
  For i As Integer = 0 To 9
    strArray(i) = CStr(dv.Item(i).Item("number"))
  Next i
  Return strArray
End Function