我必须将数据放入字典中,从而形成:Dictionary(Of Integer,List(Of 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
答案 0 :(得分:0)
您需要创建RicercaNews的新实例,并在内部循环的每次迭代中将其存储在变量ricnews中。目前,您只需创建一个实例并在每次迭代时更改它的属性。
此外,我无法看到Me.GetFigliCategory(ck.Key)
正在做什么,但我怀疑你不想跳过对象构造,如果它返回0.相反,你不应该创建新列表,如果它返回0和然后创建对象并将其创建到每次迭代上的列表,而不管Me.GetFigliCategory(ck.Key)`是否返回0?
答案 1 :(得分:0)
您好,感谢您的回答,您说我输入此声明
Dim As New ricnews 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.GetFigliCategory(ck.Key)说如果偶然孩子的父亲,因为我的数据库有类别和子类别,有孩子的类别无法消除