Question

How can I rewrite this function so it will do the same like now, but without using LINQ?

Public Function GetAllConnections() As IEnumerable(Of Connection)
    Return GetAllTcpConnections.Concat(GetAllUdpConnections) _
                               .OrderBy(Function(Conn) Conn.Proto) _
                               .ThenBy(Function(Conn) Conn.State)
End Function

Both functions, GetAllTcpConnections and GetAllUdpConnections return a As List(Of Connection)

I basiclly need this function to do the same thing like now, without using LINQ, so I can also use it with Net Framework 2.0

Was it helpful?

Solution

As my comment, I would suggest you to use LINQBridge, however you don't seem to want to use LINQ.

Below is an example how you could solve this. First do the concat yourself and afterwards use a custom comparer to sort.

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

Note that the above example will output the same as the code using LINQ. However under the hood, the implementation is quite different (using Lists instead of IEnumerable (LINQ)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top