gpt4 book ai didi

vb.net - VB.net 中的 LINQ 语句

转载 作者:行者123 更新时间:2023-12-02 01:50:13 25 4
gpt4 key购买 nike

对于我的类(class)项目,我必须创建一个成绩簿并编写 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

最佳答案

实现Icomparable接口(interface),将display(gradeBooks, "Ascending Order")改为display(ascending, "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

关于vb.net - VB.net 中的 LINQ 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com