I have to put data into a dictionary, thus formed: Dictionary (Of Integer, List (Of RicercaNews))
but when I go to put the list in the dictionary, I always return the same record, as if it does not increase the index of the list.
This is the code:
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
You need to reate a new instance of RicercaNews and store it in the variable ricnews on each iteration of your inner most loop. At present you’re creating just one instance and changing it’s properties on every iteration.
Also, I can’t see what
Me.GetFigliCategory(ck.Key)is doing but I suspect you don’t want to skip the object construction if it returns 0. Instead shouldn’t you create the new list if it returns 0 and then create the object and it to the list on every iteration, irresepective of whether Me.GetFigliCategory(ck.Key)` returned 0 or not?