c# 中的唯一排列不提供重复但 vb.net 提供
Unique permutation in c# gives no dup but vb.net does
所以我正在转换一个扩展方法来查找通用列表的排列,从 c# 到 vb
c# 中的代码不 return 重复。例如:{1,2} 与 {2,1} 相同,不允许使用 {1,1}。但是,我转换的 vb 代码给了我一个副本。有人可以帮我发现问题吗?我从这个线程中接受的答案中得到了 c# 代码:How to Generate Combinations of Elements of a List<T> in .NET 4.0
这里是 vb 代码:
Module HelperPermutation
<Extension()>
Public Function Combinations(Of T)(elements As IEnumerable(Of T), k As Integer) As IEnumerable(Of T())
Dim result As List(Of T()) = New List(Of T())
If (k = 0) Then
result.Add(New T(-1) {})
Else
Dim current As Integer = 1
For Each element In elements
result.AddRange(elements _
.Skip(current = current + 1) _
.Combinations(k - 1) _
.Select(Function(c) (New T() {element}).Concat(c).ToArray())
)
Next
End If
Return result
End Function
End module
我尝试添加 Distinct,但它仍然给我重复项。
这是我使用该扩展方法的控制台应用程序:
Dim list As New List(Of Integer) From {1, 2, 3}
Dim array = list.Combinations(2)
Console.WriteLine("# of permutation: " & array.Count)
For Each value In array
Console.WriteLine("-------Pairs: " & value.Count)
For i As Integer = 0 To value.Count - 1 Step 1
Console.WriteLine("value = " & value.ElementAt(i))
Next
Next
Console.ReadLine()
current = current + 1
没有进行赋值。这是一个相等性测试,所以该表达式的结果是布尔值。由于 Skip()
没有采用布尔值的重载,因此您似乎没有使用 Option Strict。我强烈建议使用它来发现这样的错误。
VB 语言中没有内置 post 增量,但幸运的是,您可以创建自己的增量。
Function PostIncrement(ByRef arg As Integer) As Integer
arg += 1
Return arg - 1
End Function
所以我正在转换一个扩展方法来查找通用列表的排列,从 c# 到 vb c# 中的代码不 return 重复。例如:{1,2} 与 {2,1} 相同,不允许使用 {1,1}。但是,我转换的 vb 代码给了我一个副本。有人可以帮我发现问题吗?我从这个线程中接受的答案中得到了 c# 代码:How to Generate Combinations of Elements of a List<T> in .NET 4.0
这里是 vb 代码:
Module HelperPermutation
<Extension()>
Public Function Combinations(Of T)(elements As IEnumerable(Of T), k As Integer) As IEnumerable(Of T())
Dim result As List(Of T()) = New List(Of T())
If (k = 0) Then
result.Add(New T(-1) {})
Else
Dim current As Integer = 1
For Each element In elements
result.AddRange(elements _
.Skip(current = current + 1) _
.Combinations(k - 1) _
.Select(Function(c) (New T() {element}).Concat(c).ToArray())
)
Next
End If
Return result
End Function
End module
我尝试添加 Distinct,但它仍然给我重复项。 这是我使用该扩展方法的控制台应用程序:
Dim list As New List(Of Integer) From {1, 2, 3}
Dim array = list.Combinations(2)
Console.WriteLine("# of permutation: " & array.Count)
For Each value In array
Console.WriteLine("-------Pairs: " & value.Count)
For i As Integer = 0 To value.Count - 1 Step 1
Console.WriteLine("value = " & value.ElementAt(i))
Next
Next
Console.ReadLine()
current = current + 1
没有进行赋值。这是一个相等性测试,所以该表达式的结果是布尔值。由于 Skip()
没有采用布尔值的重载,因此您似乎没有使用 Option Strict。我强烈建议使用它来发现这样的错误。
VB 语言中没有内置 post 增量,但幸运的是,您可以创建自己的增量。
Function PostIncrement(ByRef arg As Integer) As Integer
arg += 1
Return arg - 1
End Function