Vb.Net 中的数据表列到字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26502253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:22:42  来源:igfitidea点击:

DataTable Column to String Array in Vb.Net

vb.netautocompletedatatable

提问by DareDevil

I am working with DataTable Column, I want to fetch a single column of datatable to String array and then wanted to assign to AutoCompleteStringCollectionobject. at the moment I am populating 'AutoCompleteStringCollection' using a loop, but if I have more then 500 rows, it takes time. I want to do this with out loop. can any body suggest a better quicker way.

我正在使用 DataTable Column,我想将单列数据表提取到 String 数组,然后想分配给AutoCompleteStringCollection对象。目前我正在使用循环填充“AutoCompleteStringCollection”,但如果我有超过 500 行,则需要时间。我想用外循环来做到这一点。任何机构都可以提出更好更快的方法。

Dim autocomp As New AutoCompleteStringCollection
        For index As Integer = 0 To unionTable.Rows.Count - 1
            autocomp.Add(unionTable.Rows(index)(1).ToString())
        Next

I want to do without loop, help please

我想做没有循环,请帮忙

After a little struggle I managed to do this.with .Net 3.5 framework

经过一番挣扎,我设法做到了这一点。使用 .Net 3.5 框架

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim table1 As DataTable = New DataTable("Test")
        table1.Columns.Add("name")
        table1.Columns.Add("id")
        table1.Rows.Add("Abb", 1)
        table1.Rows.Add("Killers", 2)

        Dim allAutoCompletes = From row In table1.AsEnumerable()
                       Let autoComplete = row.Field(Of String)(0)
                       Select autoComplete
        Dim autoCompleteString As String() = allAutoCompletes.ToArray()
        Dim x As String = ""
        Dim autocomp As New AutoCompleteStringCollection
        autocomp.AddRange(autoCompleteString)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

回答by Tim Schmelter

You always have to use a loop. But you can use LINQ which looks nicer:

你总是必须使用循环。但是您可以使用看起来更好的 LINQ:

Dim allAutoCompletes = From row In unionTable.AsEnumerable()
                       Let autoComplete = row.Field(Of String)(1)
                       Select autoComplete 
Dim autoCompleteStringCollection As String() = allAutoCompletes.ToArray()

If you want a List(Of String)use ToList.

如果你想List(Of String)使用ToList.

Here's the same as above in method syntax, use what you find more readable:

这里的方法语法与上面相同,使用你觉得更易读的内容:

Dim autoCompleteStringCollection As String() = unionTable.AsEnumerable().
    Select(Function(r) r.Field(Of String)(1)).
    ToArray()

回答by Daniel P

The code from Rango works great for me. Here's the code as an extension which can be placed in any public module and used by adding "Imports Extensions" in the class you wish to enable it for:

Rango 的代码对我很有用。这是作为扩展的代码,它可以放置在任何公共模块中,并通过在您希望启用它的类中添加“导入扩展”来使用:

Public Module Extensions
    ''' <summary>Build string array from specified column in DataTable.</summary>
    ''' <returns>String()</returns>
    <Extension()>
    Public Function ColumnToStringArray(ByVal dataTable As DataTable, columnIndex As Integer) As String()
        Dim allAutoCompletes = From row In dataTable.AsEnumerable()
                           Let autoComplete = row.Field(Of String)(columnIndex)
                           Select autoComplete
        Return allAutoCompletes.ToArray()
    End Function
End Module

Use:

用:

Dim dt as DataTable = (function that returns datatable)
TextBox1.AutoCompleteCustomSource.AddRange(dt.ColumnToStringArray(0))