您好我有以下功能:
Public Shared Function GetAbrechnung() As List(Of WochenBPlan)
Dim wbp = New WochenBPlan
Dim allBookings As List(Of WochenBPlan) = New List(Of WochenBPlan)
Dim strSQL As String = "SELECT DISTINCT d.ABTEILUNG, d.BEMERKUNG, d.BESTUHLUNG, d.RAUMID, d.ID, d.PERSONENZAHL, d.ADRESSE + ', ' + d.RAUMBEZEICHNUNG AS RAUMBEZEICHNUNG, d.THEMA, p.VN + ' ' + p.NN AS NAME, CONVERT (char(5), d.VON, 108) + ' - ' + CONVERT (char(5), d.BIS, 108) AS ZEIT, p.TEL FROM VIEW_RAUMBUCHUNG_DISPO AS d INNER JOIN PERSONAL AS p ON d.PERSONAL_ID = p.ID WHERE THEMA = 'EVENT'"
Dim objRS As SqlDataReader
objRS = SQLrunReaderWB(strSQL)
If objRS.HasRows Then
While objRS.Read()
wbp.Raum = objRS("RAUMBEZEICHNUNG")
wbp.Zeit = objRS("ZEIT")
If Not IsDBNull(objRS("ABTEILUNG")) Then
wbp.Bereich = objRS("ABTEILUNG")
End If
If Not IsDBNull(objRS("THEMA")) Then
wbp.Thema = objRS("THEMA")
End If
If Not IsDBNull(objRS("NAME")) Then
wbp.Mieter = objRS("NAME")
End If
If Not IsDBNull(objRS("TEL")) Then
wbp.Mieter_Tel = objRS("TEL")
End If
wbp.Personen = objRS("PERSONENZAHL")
wbp.Bestuhlung = objRS("BESTUHLUNG")
If Not IsDBNull(objRS("BEMERKUNG")) Then
wbp.Bemerkung = objRS("BEMERKUNG")
End If
allBookings.Add(wbp)
End While
Else
End If
ConnWB.Close()
Return allBookings
End Function
我的问题是我生成的列表只是返回最后一个值。我做错了什么?有人可以帮忙吗?
答案 0 :(得分:2)
您需要在循环内声明范围对象。如果不这样做,您将只获得相同对象的引用,更改它,因此将是包含该列表的最后一个修改。
尝试这样的事情:
Public Shared Function GetAbrechnung() As List(Of WochenBPlan)
Dim allBookings As List(Of WochenBPlan) = New List(Of WochenBPlan)
Dim strSQL As String = "SELECT DISTINCT d.ABTEILUNG, d.BEMERKUNG, d.BESTUHLUNG, d.RAUMID, d.ID, d.PERSONENZAHL, d.ADRESSE + ', ' + d.RAUMBEZEICHNUNG AS RAUMBEZEICHNUNG, d.THEMA, p.VN + ' ' + p.NN AS NAME, CONVERT (char(5), d.VON, 108) + ' - ' + CONVERT (char(5), d.BIS, 108) AS ZEIT, p.TEL FROM VIEW_RAUMBUCHUNG_DISPO AS d INNER JOIN PERSONAL AS p ON d.PERSONAL_ID = p.ID WHERE THEMA = 'EVENT'"
Dim objRS As SqlDataReader
objRS = SQLrunReaderWB(strSQL)
If objRS.HasRows Then
While objRS.Read()
'declare here a new instance to add on the list
Dim wbp = New WochenBPlan
wbp.Raum = objRS("RAUMBEZEICHNUNG")
wbp.Zeit = objRS("ZEIT")
If Not IsDBNull(objRS("ABTEILUNG")) Then
wbp.Bereich = objRS("ABTEILUNG")
End If
If Not IsDBNull(objRS("THEMA")) Then
wbp.Thema = objRS("THEMA")
End If
If Not IsDBNull(objRS("NAME")) Then
wbp.Mieter = objRS("NAME")
End If
If Not IsDBNull(objRS("TEL")) Then
wbp.Mieter_Tel = objRS("TEL")
End If
wbp.Personen = objRS("PERSONENZAHL")
wbp.Bestuhlung = objRS("BESTUHLUNG")
If Not IsDBNull(objRS("BEMERKUNG")) Then
wbp.Bemerkung = objRS("BEMERKUNG")
End If
' add the reference to the new object on the list
allBookings.Add(wbp)
End While
'Else ---> you do not need this ELSE scope, just remove it
End If
ConnWB.Close()
Return allBookings
End Function
答案 1 :(得分:2)
您需要在每次循环迭代中创建一个新的WochenBPlan实例。否则,您只是更改替换以前设置的值。将其添加到循环中。
wbp = New WochenBPlan()