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

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • 4
    Well, you're calling `ToString()` on a `List`. How were you *expecting* that to perform a conversion, and why? (I'd strongly recommend building all the text in a StringBuilder first, and then setting da.Text at the end, btw.) – Jon Skeet Aug 11 '15 at 17:40
  • Related: [Question regarding C#'s `List<>.ToString`](http://stackoverflow.com/questions/5050507/question-regarding-cs-list-tostring), but I'm pretty sure this is a duplicate of something out there. – crashmstr Aug 11 '15 at 17:54
  • [C# System.Collections.Generic.List`1System.String](http://stackoverflow.com/questions/16106181/c-sharp-system-collections-generic-list1system-string) – crashmstr Aug 11 '15 at 17:55
  • [System.Collections.Generic.List`1test.Node](http://stackoverflow.com/questions/23069734/system-collections-generic-list1test-node) – crashmstr Aug 11 '15 at 17:56
  • [System.Collections.Generic.List`1System.String issue in mvc4](http://stackoverflow.com/questions/26713262/system-collections-generic-list1system-string-issue-in-mvc4) – crashmstr Aug 11 '15 at 17:57

5 Answers5

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));
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
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();
Dai
  • 141,631
  • 28
  • 261
  • 374
0

I think what you actually want is this:

Dictionary< int, List<int> >

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

Dai
  • 141,631
  • 28
  • 261
  • 374
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;
Piotr Dory
  • 334
  • 2
  • 12
  • Oh true, as Dai posted, you are making dictionary of lists of arrays of ints – Piotr Dory Aug 11 '15 at 17:45
  • This will still print the signature of the arrays, because `kvp.Value` is a list of `Array`s, what you need instead is to flatten the ints by doing `kvp.Value.SelectMany(i => i)` – maraaaaaaaa Aug 11 '15 at 17:48
  • Yeah I noticed that it is Dictionary>, not Dictionary>. Well, there are already other answers which will work – Piotr Dory Aug 11 '15 at 17:52
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/>

Mister Epic
  • 16,295
  • 13
  • 76
  • 147