对于我的班级项目,我必须创建一个成绩册并编写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
发布于 2014-04-23 13:52:46
实现接口Icomparable,并将display(gradeBooks,升序)改为display(升序,升序)。
这段代码对我来说很有效。
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
发布于 2014-04-23 13:52:54
对于按分数排序,您需要请求按分数排序
Dim ordered =
From value In gradeBooks
Order By value.Score
Select value
display(ordered, "Ascending Order")
(我还更改了IEnumerable的名称,以避免与关键字“升序”混淆)
在提取传递的可枚举对象时也需要进行相同的更改
Dim passed =
From gradeBook In gradeBooks
Where gradeBook.Score > 60
Order By gradeBook.Score
Select gradeBook
display(passed, "Students who passed: ")
您的实际代码是将整个成绩册作为对象传递给order by。在这种情况下,您需要实现(如其他答案中所述) IEnumerable接口,以告诉编译器如何将一个成绩册与另一个成绩册进行比较,以便对它们进行排序。但这里可能并不真正需要这样做,只需将字段(数字或字符串)传递给排序依据就足够了
发布于 2014-04-23 14:03:55
您之所以看到相同的信息重复,是因为您在同一集合上调用了display两次。一旦纠正了这一点,你应该抛出一个异常,因为你正在尝试按整个对象排序,而不是按对象的属性/字段排序。
https://stackoverflow.com/questions/23245936
复制