如何:创建项列表

本主题中的代码定义一个 Student 类,并创建该类的实例列表。 该列表用于支持主题演练:用 Visual Basic 编写查询。 该列表还可用于需要对象列表的任何应用程序。 该代码通过使用对象初始值设定项来定义学生列表中的项。

示例

如果您正在使用演练,则可以将此代码用于在该处创建的项目的 Module1.vb 文件。 只需将 Main 方法中使用 **** 标记的行替换为演练中提供的查询和查询执行。

Module Module1

    Sub Main()
        ' Create a list of students.
        Dim students = GetStudents()
        ' Display the names in the list.
        DisplayList(students)
        ' ****Paste query and query execution code from the walkthrough,
        ' ****or any code of your own, here in Main.
        Console.ReadLine()
    End Sub

    ' Call DisplayList to see the names of the students in the list.
    ' You can expand this method to see other student properties.
    Sub DisplayList(ByVal studentCol As IEnumerable(Of Student))
        For Each st As Student In studentCol
            Console.WriteLine("First Name: " & st.First)
            Console.WriteLine(" Last Name: " & st.Last)
            Console.WriteLine()
        Next
    End Sub

    ' Function GetStudents returns a list of Student objects.
    Function GetStudents() As IEnumerable(Of Student)
        Return New List(Of Student) From
            {
             New Student("Michael", "Tucker", "Junior", 10),
             New Student("Svetlana", "Omelchenko", "Senior", 2),
             New Student("Michiko", "Osada", "Senior", 7),
             New Student("Sven", "Mortensen", "Freshman", 53),
             New Student("Hugo", "Garcia", "Junior", 16),
             New Student("Cesar", "Garcia", "Freshman", 4),
             New Student("Fadi", "Fakhouri", "Senior", 72),
             New Student("Hanying", "Feng", "Senior", 11),
             New Student("Debra", "Garcia", "Junior", 41),
             New Student("Lance", "Tucker", "Junior", 60),
             New Student("Terry", "Adams", "Senior", 6)
            }
    End Function

    ' Each student has a first name, a last name, a class year, and 
    ' a rank that indicates academic ranking in the student body.
    Public Class Student
        Public Property First As String
        Public Property Last As String
        Public Property Year As String
        Public Property Rank As Integer

        Public Sub New()

        End Sub

        Public Sub New(ByVal firstName As String,
                       ByVal lastName As String,
                       ByVal studentYear As String,
                       ByVal studentRank As Integer)
            First = firstName
            Last = lastName
            Year = studentYear
            Rank = studentRank
        End Sub
    End Class
End Module

请参见

任务

演练:用 Visual Basic 编写查询

概念

对象初始值设定项:命名类型和匿名类型 (Visual Basic)

Visual Basic 中的 LINQ 简介

其他资源

Video How to: Writing Queries in Visual Basic

Visual Basic 中的 LINQ 入门

Visual Basic 中的 LINQ

查询 (Visual Basic)