对于我的课程项目,我必须创建成绩簿并编写LINQ语句以列出所有学生,按升序显示,并显示通过测试的每个人。我的所有陈述都返回了相同的信息,但情况并非如此。这是我的代码。
Public Class GradeBook
Private nameValue As String
Private scoreValue As Integer
Public Sub New(ByVal n As String, ByVal s As Integer)
nameValue = n
scoreValue = s
End Sub
Public Property Name() As String
Get
Return nameValue
End Get
Set(ByVal value As String)
nameValue = value
End Set
End Property
Public Property Score() As Integer
Get
Return scoreValue
End Get
Set(ByVal value As Integer)
scoreValue = value
End Set
End Property
Public Sub displayGradeBook()
Console.WriteLine("Name: " & Name & vbTab & "Score: " & Score)
End Sub
End Class
Sub Main()
Dim g1 As New GradeBook("AAA", 70)
Dim g2 As New GradeBook("BBB", 50)
Dim g3 As New GradeBook("CCC", 100)
Dim g4 As New GradeBook("DDD", 80)
'add g1, g2, g3 and g4 in a array and display all student scores
Dim gradeBooks As GradeBook() = {g1, g2, g3, g4}
display(gradeBooks, "Scores for all students: ")
'create a LINQ which get all scores in ascending order and display them.
Dim ascending =
From value In gradeBooks
Order By value Ascending
Select value
display(gradeBooks, "Ascending Order")
'create a LINQ which get all students who passed the exam
Dim passed =
From gradeBook In gradeBooks
Where gradeBook.Score > 60
Order By gradeBook
Select gradeBook
display(passed, "Students who passed: ")
'display number of passed students, their names and scores
End Sub
'display gradeBook's information
Private Sub display(ByVal gradeBooks As IEnumerable, ByVal header As String)
Console.WriteLine(header)
For Each g As GradeBook In gradeBooks
g.displayGradeBook()
Next
Console.WriteLine()
Console.ReadLine()
End Sub
End Module
答案 0 :(得分:2)
实现Icomparable接口,并更改显示(gradeBooks," Ascending Order")以显示(升序,"升序")。
这段代码对我有用。
Module Module1
Public Class GradeBook
Implements IComparable(Of GradeBook)
Private nameValue As String
Private scoreValue As Integer
Public Sub New(ByVal n As String, ByVal s As Integer)
nameValue = n
scoreValue = s
End Sub
Public Property Name() As String
Get
Return nameValue
End Get
Set(ByVal value As String)
nameValue = value
End Set
End Property
Public Property Score() As Integer
Get
Return scoreValue
End Get
Set(ByVal value As Integer)
scoreValue = value
End Set
End Property
Public Sub displayGradeBook()
Console.WriteLine("Name: " & Name & vbTab & "Score: " & Score)
End Sub
Public Function CompareTo(ByVal other As GradeBook) As Integer Implements System.IComparable(Of GradeBook).CompareTo
'-1 = less than other; 0 = same as other; +1 = greater than other'
If IsNothing(other) Then
Return 1
End If
If Me.Score > other.Score Then
Return 1
End If
If Me.Score < other.Score Then
Return -1
End If
Return 0
End Function
End Class
Sub Main()
Dim g1 As New GradeBook("AAA", 70)
Dim g2 As New GradeBook("BBB", 50)
Dim g3 As New GradeBook("CCC", 100)
Dim g4 As New GradeBook("DDD", 80)
'add g1, g2, g3 and g4 in a array and display all student scores
Dim gradeBooks As GradeBook() = {g1, g2, g3, g4}
display(gradeBooks, "Scores for all students: ")
'create a LINQ which get all scores in ascending order and display them.
Dim ascending =
From value In gradeBooks
Order By value Ascending
Select value
display(ascending, "Ascending Order")
'create a LINQ which get all students who passed the exam
Dim passed =
From gradeBook In gradeBooks
Where gradeBook.Score > 60
Order By gradeBook
Select gradeBook
display(passed, "Students who passed: ")
'display number of passed students, their names and scores
End Sub
'display gradeBook's information
Private Sub display(ByVal gradeBooks As IEnumerable, ByVal header As String)
Console.WriteLine(header)
For Each g As GradeBook In gradeBooks
g.displayGradeBook()
Next
Console.WriteLine()
Console.ReadLine()
End Sub
End Module
答案 1 :(得分:0)
对于按分数订购的订单,您需要按分数请求订购
Dim ordered =
From value In gradeBooks
Order By value.Score
Select value
display(ordered, "Ascending Order")
(我还更改了IEnumerable的名称以避免与关键字Ascending混淆)
在提取传递的可枚举
时也需要进行相同的更改Dim passed =
From gradeBook In gradeBooks
Where gradeBook.Score > 60
Order By gradeBook.Score
Select gradeBook
display(passed, "Students who passed: ")
您的实际代码是将整个成绩单作为对象传递给。通过这种方式,您需要实现(如在其他答案中所解释的)IEnumerable接口,以教授编译器如何将成绩单与另一个成绩簿进行比较以对其进行排序。但这可能不是真的需要在这里,只需将字段(数字或字符串)传递给订单就足够了
答案 2 :(得分:0)
您重复看到相同信息的原因是您在同一个集合上调用了两次显示。一旦纠正,你应该抛出一个异常,因为你试图整个对象而不是对象的属性/字段。