0

I am trying to display the values from a dictionary function but it does not display the value, what it shows is below -

1007 System.Collections.Generic.List`1[System.Int32[]] 

1006 System.Collections.Generic.List`1[System.Int32[]] 

1009 System.Collections.Generic.List`1[System.Int32[]] 

1008 System.Collections.Generic.List`1[System.Int32[]]

the code I am using is shown below

    clsCollaborativeFilter mri = new clsCollaborativeFilter();
    Dictionary<int, List<int[]>> movRecommendations = mri.aList1();

    foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
    {
        da.Text += kvp.Key;
        da.Text += " ";
        da.Text += kvp.Value;
        da.Text += "<br/>";
    }
    return da.Text;

I cant seem to understand why this is happening

5

5 Answers 5

1

you are adding a List of an Array of ints to a string, in the line: da.Text += kvp.Value; so what it is doing, is it is adding the signature of the List of Arrays because it doesn't know exactly what you are trying to do.

What you can do instead is:

da.Text += String.Join(", ", kvp.Value.SelectMany(i => i));
Sign up to request clarification or add additional context in comments.

1 Comment

yea that seem to work now I am able to see the values I required so thanks
1

...or assuming that it really is a dictionary-of-list-of-int-arrays, then:

StringBuilder sb = new StringBuilder();
foreach(Int32 key in movRecommendations.Key) {
    List<Int32[]> listOfArrays = movRecommendations[ key ];

    sb.Append( key );
    sb.AppendLine();
    foreach(Int32[] array in listOfArrays) {
        Boolean isFirst = true;
        foreach(Int32 val in array) {
            if( !isFirst ) sb.Append( ", " );
            sb.Append( val );
            isFirst = false;
        }
        sb.AppendLine();
    }
    sb.AppendLine("<br />");
}
da.Text = sb.ToString();

Comments

0

I think what you actually want is this:

Dictionary< int, List<int> >

...a List<int[]> doesn't make much sense to me.

Comments

0
clsCollaborativeFilter mri = new clsCollaborativeFilter();
Dictionary<int, List<int[]>> movRecommendations = mri.aList1();

foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
{
    da.Text += kvp.Key;
    da.Text += " ";
    da.Text += string.Join(",",kvp.Value);
    da.Text += "<br/>";
}
return da.Text;

3 Comments

Oh true, as Dai posted, you are making dictionary of lists of arrays of ints
This will still print the signature of the arrays, because kvp.Value is a list of Arrays, what you need instead is to flatten the ints by doing kvp.Value.SelectMany(i => i)
Yeah I noticed that it is Dictionary<int, List<int[]>>, not Dictionary<int, List<int>>. Well, there are already other answers which will work
0
Dictionary<int, List<int[]>> movRecommendations = new Dictionary<int, List<int[]>>(){
    {0, new List<int[]>(){ new int[]{1, 2, 3} }},
    {1, new List<int[]>(){ new int[]{7, 8, 9} }}
};
string da = String.Empty; 
foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
{
    da += kvp.Key;
    da += " ";
    da += String.Join(", ", kvp.Value.SelectMany(i => i));
    da += "<br/>";
}
Console.WriteLine (da);

This will write out 0 1, 2, 3<br/>1 7, 8, 9<br/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.