2012年1月10日 星期二

#LinQ Linq 基本語法 – OrderBy 與 OrderByDescending


Linq 基本語法 – OrderBy 與 OrderByDescending



    Structure Pet
        Public Name As String
        Public Age As Integer
    End Structure

    Sub OrderByEx1()
        ' Create an array of Pet objects.
        Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                             New Pet With {.Name = "Boots", .Age = 4}, _
                             New Pet With {.Name = "Whiskers", .Age = 1}}

        ' Order the Pet objects by their Age property.
        Dim query As IEnumerable(Of Pet) = _
            pets.OrderBy(Function(pet) pet.Age)

        Dim output As New System.Text.StringBuilder
        For Each pt As Pet In query
            output.AppendLine(pt.Name & " - " & pt.Age)
        Next

        ' Display the output.
        MsgBox(output.ToString())
    End Sub

沒有留言:

張貼留言