我如何重写这个函数,这样它就会像现在一样,但不使用LINQ?
Public Function GetAllConnections() As IEnumerable(Of Connection)
Return GetAllTcpConnections.Concat(GetAllUdpConnections) _
.OrderBy(Function(Conn) Conn.Proto) _
.ThenBy(Function(Conn) Conn.State)
End Function
函数GetAllTcpConnections和GetAllUdpConnections都返回一个As List(Of Connection
)
我基本上需要这个函数做同样的事情,就像现在一样,不使用LINQ,所以我也可以使用它与。Net Framework 2.0
作为我的评论,我建议你使用LINQBridge,但是你似乎不想使用LINQ。
下面是一个如何解决这个问题的例子。首先自己进行concat,然后使用自定义比较器进行排序。
Class ConnectionComparer
Implements IComparer(Of Connection)
Public Function Compare(x As Connection, y As Connection) As Integer Implements System.Collections.Generic.IComparer(Of Connection).Compare
' Assuming that "Nothing" < "Something"
If x Is Nothing AndAlso y Is Nothing Then Return 0
If x Is Nothing AndAlso y IsNot Nothing Then Return 1
If x IsNot Nothing AndAlso y Is Nothing Then Return -1
Dim protoCompare As Integer = x.Proto.CompareTo(y.Proto)
If protoCompare = 0 Then
Return x.State.CompareTo(y.State)
Else
Return protoCompare
End If
End Function
End Class
Function GetAllConnections() As IEnumerable(Of Connection)
' Concat
Dim connections As List(Of Connection) = GetAllTcpConnections()
connections.AddRange(GetAllUdpConnections())
' Custom comparer to compare first "Proto" and then "State"
Dim comparer As New ConnectionComparer()
connections.Sort(comparer)
Return connections
End Function
注意,上面的示例将输出与使用LINQ的代码相同的结果。然而,在底层,实现是完全不同的(使用列表而不是IEnumerable (LINQ))。