Asp.net 在vb.net中将数据添加到字典列表中

Asp.net 在vb.net中将数据添加到字典列表中,asp.net,vb.net,Asp.net,Vb.net,我必须将数据放入一个字典,这样就形成了:字典(整数的,列表(RicercaNews的)) 但是,当我将列表放入字典时,我总是返回相同的记录,好像它不会增加列表的索引 代码如下: ResultTable Dim As New DataTable () Dictgestionalews Dim As New Dictionary (Of Integer, List (Of RicercaNews)) Listacat Dim As New Dict

我必须将数据放入一个字典,这样就形成了:字典(整数的,列表(RicercaNews的))

但是,当我将列表放入字典时,我总是返回相同的记录,好像它不会增加列表的索引

代码如下:

ResultTable Dim As New DataTable () 
            Dictgestionalews Dim As New Dictionary (Of Integer, List (Of RicercaNews)) 
            Listacat Dim As New Dictionary (Of Integer, String) 
            Dim listaricerca As List (Of RicercaNews) 
            Dim As New ricnews RicercaNews () 
            Try 
                listacat Me.GetAllCategoryNews = () 
                Dim comm As DbCommand = GenericDataAccess.CreateCommand () 


                For Each ck As KeyValuePair (Of Integer, String) In listacat 
                    comm.CommandText = "SELECT tabNews.idNews, tabNews.titolo, tabNews.commenti, tabNews.DataInizio, tabNews.DataFine, tabNews.idcatnews" _ 
                    & "FROM WHERE tabNews tabNews.idcatnews =" & ck.Key 
                    resultTable GenericDataAccess.ExecuteSelectCommand = (comm) 

                    If Me.GetFigliCategory (ck.Key)> 0 Then 
                        listaricerca = New List (Of RicercaNews) 

                        For index As Integer = 0 To resultTable.Rows (). Count () - 1 

                            ricnews.DataFine resultTable.Rows = (). Item (index). Item ("EndDate") 
                            ricnews.DataInizio resultTable.Rows = (). Item (index). Item ("StartDate") 
                            ricnews.idnews resultTable.Rows = (). Item (index). Item ("idNews) 
                            ricnews.titolo resultTable.Rows = (). Item (index). Item ("title") 
                            ricnews.idcatnews resultTable.Rows = (). Item (index). Item ("idcatnews) 
                            listaricerca.Add (ricnews) 
                        Next 



                        dictgestionalews.Add (ck.Key, listaricerca) 
                    Else 
                        dictgestionalews.Add (ck.Key New List (Of RicercaNews) ()) 

                    End If 

                Next 


                Return dictgestionalews 

            Catch ex As Exception 
                Utilita.LogError (former) 

                Return Nothing 
            End Try 

您需要创建一个新的RicercaNews实例,并在最内部循环的每次迭代中将其存储在变量ricnews中。目前,您只创建一个实例,并在每次迭代中更改它的属性


另外,我看不到
Me.getfiglicontegory(ck.Key)
在做什么,但我怀疑如果对象构造返回0,您不想跳过它。相反,如果它返回0,那么您不应该创建新列表,然后在每次迭代时创建对象并将其添加到列表中,而不考虑Me.getFiglicontegory(ck.Key)`是否返回0?

您好,谢谢您的回答,您说我输入了这条语句

暗淡如新的RiceNews RicercaNews()

在循环内部,如下所示:

For index As Integer = 0 To resultTable.Rows (). Count () - 1
                            **Dim As New ricnews RicercaNews ()**
                            ricnews.DataFine resultTable.Rows = (). Item (index). Item ("EndDate")
                            ricnews.DataInizio resultTable.Rows = (). Item (index). Item ("StartDate")
                            ricnews.idnews resultTable.Rows = (). Item (index). Item ("idNews)
                            ricnews.titolo resultTable.Rows = (). Item (index). Item ("title")
                            ricnews.idcatnews resultTable.Rows = (). Item (index). Item ("idcatnews)
                            listaricerca.Add (ricnews)
                        Next

函数Me.getfiglicograge(ck.Key)表示,如果碰巧是孩子的父亲,因为my db有类别和子类别,无法删除有孩子的类别

请通过编辑将任何新信息添加到您的问题中。如果您想讨论某个答案,请使用与该答案相关联的添加评论功能。如果提供的答案解决了您的问题,请单击答案旁边的勾号接受。