0

I'm trying to add multiple generic list objects to the same key in my dictionary. How do I add multiple generic list objects without getting an error that says my list cannot be converted to an object. Also, I'm not trying to add items to one list. I'm trying to have multiple separate lists that are assigned to a key so I can access it when creating separate tables in my web application.

I am a newbie to c# so please help in terms of understanding what I should be doing. I've tried to say myDict[id].Add(List<object>) but still get an error that the list cannot be added.

Dictionary<int, `List<Roll>> myDict = new Dictionary<int, List<Roll>>();
 if (myDict.ContainsKey(ID))
     {               
        myDict[ID].Add(_WorkRolls);
        myDict[ID].Add(_UsedRolls);

      }
      else
      {
        myDict.Add(ID, _OldRolls);
      }

My error keeps giving back that "CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'Roll'

UPDATE: I decided to just use 2-D Lists. Thanks!

Rae
  • 25
  • 10
  • You myDict has a key of int and value of List, it looks like you are trying to add a type that is not of the correct type by calling myDict[ID].Add(_WorkRolls) and myDict[ID].Add(_UsedRolls). What are the WorkRolls and UsedRolls Types? – Jake Steffen Jun 05 '19 at 16:25
  • 2
    How about `AddRange` instead `Add` seems you want to add some items not a single one right? (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.8) – k1ll3r8e Jun 05 '19 at 16:25
  • Dictionaries allow one entry per key. If you need more than one, try using a [Lookup](https://stackoverflow.com/questions/1403493/what-is-the-point-of-lookuptkey-telement) instead. – John Wu Jun 05 '19 at 16:26
  • Work Rolls and Used Rolls are List type. I want to add separate multiple lists to a single key because I'll be iterating through my dictionary to create a table for each list. – Rae Jun 05 '19 at 16:27
  • Something's wrong with that error. If you were trying to add the wrong type to the dictionary, the error would be `cannot convert from 'Roll' to 'System.Collections.Generic.List'`, not the other way around. Is the compiler error you're describing in the code shown? And, after fixing the compiler error, you'll get a runtime error from trying to insert the same key twice. – Scott Hannen Jun 05 '19 at 17:18
  • This is the error: `CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'Status.DataModels.Roll' ` – Rae Jun 05 '19 at 17:53

1 Answers1

0

Since you're adding a list to a list you need to use AddRange(). Just plain Add() is used to only insert a single item.

Dictionary<int, List<Roll>> myDict = new Dictionary<int, List<Roll>>();

if (myDict.ContainsKey(ID))
{               
    myDict[ID].AddRange(_WorkRolls);
    myDict[ID].AddRange(_UsedRolls);
}
else
{
    myDict.Add(ID, _OldRolls);
}
Dortimer
  • 619
  • 8
  • 25
  • I want to add separate multiple lists to a single key because I'll be iterating through my dictionary to create a table for each list. I don't want all the items in one list because it makes it harder for me to separate the data when creating the separate tables. – Rae Jun 05 '19 at 16:28
  • I suppose you could try `Dictionary>>` to have a list of lists, but that can get very confusing very fast. Alternatively, you could add something in the `Roll` class that identifies it in some way (whether it's `_WorkRolls` or `_UsedRolls` and then do your lookup that way. What you're asking is possible, but like I said, it will get really messy really quick. – Dortimer Jun 05 '19 at 16:31
  • John Wu said to use a Lookup, would Lookup be better for me? – Rae Jun 05 '19 at 16:32
  • I think that could work as well. Again, just be cautious about having disparate lists sharing a key. It's better to make it as unique as possible with Key Value Pairs. – Dortimer Jun 05 '19 at 16:34
  • I'm just trying to figure out to group multiple lists for one ID. I'll try to lookup method and see what happens. – Rae Jun 05 '19 at 16:36