如何重写此功能,以便像现在一样,但不使用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一起使用
答案 0 :(得分:1)
作为我的评论,我建议您使用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))。