1

嗨,我有以下功能:

    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

我的问题是我生成的列表只是返回最后一个值。我在做什么错?有人可以帮忙吗?

4

2 回答 2

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
于 2012-12-20T22:50:17.727 回答
2

您需要在每次循环迭代时创建一个新的 WochenBPlan 实例。否则,您只是更改替换先前设置的值。将此添加到您的循环中。

wbp = New WochenBPlan()
于 2012-12-20T22:51:04.947 回答