简体   繁体   English

遍历数据表以创建一个看起来像这样的字符串

[英]loop through datatable to create a string that looks like this

I want to loop through my datatable column called SDESCR and created a string that looks like this. 我想遍历名为SDESCR的数据表列,并创建一个看起来像这样的字符串。

Dim labels As String() = {"North", "South", "East", "West", "Up", "Down"}

this is what i am trying and it is not working 这是我正在尝试,但不起作用

Dim labels As String() 
For Each row As DataRow In tablegraph.Rows
            labels = labels " ' " + row.Item("SDESCR") + " ',"
        Next row

THANK YOU FOR THE HELP, I WILL TEST THEM TOMORROW AND SEE WHICH WORKS BEST AND MARK ONE CORRECT. 谢谢您的帮助,我将测试它们明天的效果,并查看效果最佳和正确的一个。

Do this instead 改为这样做

Dim labels As String()
Dim labelsList As List(Of String) = New List(Of String)
For Each row As DataRow In tablegraph.Rows
  labelsList.Add(row.Item("SDESCR"))
Next

labels = labelsList.ToArray()

If you need it to be a comma delimited list instead you can just do 如果您需要将其作为逗号分隔的列表,则可以这样做

Dim commaLabels As String = String.Join(labels, ",")

If you need them stored in an array, then you can add them to a list and then call the ToArray() method on the list: 如果需要将它们存储在数组中,则可以将它们添加到列表中,然后在列表上调用ToArray()方法:

    Dim labelList As New List(Of String)
    For Each row As DataRow In tablegraph.Rows
        labelList.Add(row.Item("SDESCR"))
    Next row

    Dim labels As String() = labelList.ToArray()

If you mean a String-Array instead of a String, this works: 如果您是用String-Array而不是String表示,则可以这样做:

Dim labels(tablegraph.Rows.Count - 1) As String
For i As Int32 = 0 To tablegraph.Rows.Count - 1
    labels(i) = tablegraph(i)("SDESCR").ToString
Next

If you want them separated with a comma, you can use a StringBuilder to append: 如果希望它们之间用逗号分隔,则可以使用StringBuilder追加:

 Dim labels As New System.Text.StringBuilder
 For i As Int32 = 0 To tablegraph.Rows.Count - 1
     labels.Append(" ' ").Append(tablegraph(i)("SDESCR").ToString).Append(" ',")
 Next
 If labels.Length <> 0 Then labels.Length -= 1 'remove last comma'
 Dim labelString As String = labels.ToString 

If you want a string, use this (string builder is best due to memory management issues with constant string declaration. 如果您要一个字符串,请使用此字符串(最好使用字符串生成器,这是因为常量字符串声明中存在内存管理问题。

Dim labels As New StringBuilder()
For Each row As DataRow In tablegraph.Rows
            labels.Append(" ' " + row.Item("SDESCR") + " ',")
        Next row

Then when you need the string, use labels.ToString() 然后在需要字符串时,使用labels.ToString()

If you want an array, use this... 如果需要数组,请使用此...

Dim labels As New List(Of String)()
For Each row As DataRow In tablegraph.Rows
            labels.Add(row.Item("SDESCR"))
        Next row

Then when you need the array, use labels.ToArray() 然后在需要数组时,使用labels.ToArray()

The main reason your above code is failing is that you are declaring an Array of strings. 您上面的代码失败的主要原因是您声明了一个字符串数组。

There are plenty of ways to do this; 有很多方法可以做到这一点。 here is an approach using LINQ 这是使用LINQ的方法

Dim labels As String() = (From myRow In tablegraph _
                          Select CType(myRow.Item("SDESCR"), String)).ToArray
  • It's worth nothing that this will fail for NULL/NOTHING values of "SDESCR". 对于“ SDESCR”的NULL / NOTHING值失败,这将毫无意义。

If you just want a single comma-separated values you can do this: 如果只需要一个逗号分隔的值,则可以执行以下操作:

Dim sOutput = String.Join(",", (From myRow In tablegraph _
                                Select CType(myRow.Item("SDESCR"), String)).ToArray)
labels = labels " ' " + row.Item("SDESCR") + " ',"
               ^

Plus sign missing? 加号丢失了吗? Also, when you do this you'll be left with an extra , at the end of your string. 另外,当您执行此操作时,在字符串末尾还会有一个额外的。 So you'll want to strip that off. 因此,您将需要删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM