我有这个“结果”变量,它是一种IEnumerable
类型:
Dim result = GetCombinations(TextBox1.Text, StringLength)
要获取/写入变量的内容,我需要使用 For 迭代其中的所有项目,然后将每个项目转换为数组,如下所示:
For Each item In result
RichTextBox1.Text += vbNewLine & item.ToArray
Application.DoEvents()
Next
...所以我的回答是,如果我可以改进我的代码,例如加入 IEnumerable 内容来做类似的事情:
RichTextBox1.Text = String.Join(vbNewLine, result) ' This does not work.
我的意思是,“一次性”的事情。
如果不是,还有比 For 更好(更快)的替代方案吗?
更新
这是完整的代码:
Private Shared Function GetCombinations(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
If length = 1 Then
Return list.[Select](Function(x) New T() {x})
Else
Return GetCombinations(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Clear()
Dim result = GetCombinations("abc", 5)
' Dim result2 As IEnumerable(Of String) = result.Select(Function(item) New String(item))
' RichTextBox1.Text = String.Join(vbNewLine, result)
For Each item In result
RichTextBox1.Text &= vbNewLine & item.ToArray
' Application.DoEvents()
Next
End Sub
更新 2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Method result
Dim result As IEnumerable = Permute_Characters("abc", 2)
' Combine strings into lines
' Dont work
RichTextBox1.Text = String.Join(Environment.NewLine, result.ToString.ToArray)
End Sub