-4

I have Dictionary like this below.

The key is names like cups, pens, tape, glasses, and rocks. The values will be classes that contain things such as amount, types, images, etc.

The value is from the JSON file and contains all the values with keys associated with values.

Is it possible to have duplicate keys with different values?

For example, I want to add new values to each key and have different values with duplicated keys.

I want to get the output like

name: cup
amount: 3
colors: red
location: home //this will be the new value
name: cup
amount: 3
colors: yellow //duplicated key cup with different value
location: home //this will be the new value

New values would be location:

location: home

Is it possible?

Dictionary<string, Things> myDictionary;
public class Things{
    public string name;
    public int amount;
    public string colors;
    //...
}

foreach(Things item in thingsList.jsonfile)
{
  //go through each item and add to item key as name (cup, pens, etc) and other for values (amount, colors)
}

JSON

  "Things": [
      {
        "name": "cups",
        "amount": 5,
        "colors": "red",
        //...
      },
      {
        "name": "pens",
        "amount": 3,
        "colors": "blue",
      },
      {
        "name": "cup",
        "amount": 3,
        "colors": "yellow",
      }
    ],

I looked at this and possible to use Lookup but not sure how to use it because I am not able to find examples using Lookup with Dictionary.

Duplicate keys in .NET dictionaries?

1026
  • 1
  • 1
  • 1
  • 4
    It seems to me you could spend 10 minutes to write a test app to find out if it is possible or not. – Ken White Feb 22 '23 at 04:17
  • 2
    I don't understand your question. You want output like `the name -> cups, 5, red, home` but `cups` is the "name" according to your JSON. – Eric J. Feb 22 '23 at 04:17
  • 3
    I don't see where you have duplicates in your JSON. What *is* your dictionaries key? – MakePeaceGreatAgain Feb 22 '23 at 04:27
  • 1
    "Is it possible to have duplicate keys with different values in the dictionary?" -- No, you can't. the key should be unique – sujith karivelil Feb 22 '23 at 05:12
  • 1
    *"New values would be location: location: home"* -- I can't see any property `location` in either the JSON or the model. The corresponding property in the model seems to be the `types`, and in the JSON, `colors`. Could you edit the question and make the naming consistent? – Theodor Zoulias Feb 22 '23 at 05:16
  • 1
    make a list as the value Dictionary that way you can have as many things in there as you want. – Train Feb 22 '23 at 05:22

1 Answers1

0

As commenters have said, a Dictionary cannot have multiple keys, but it sounds actually like you want multiple values per key.

I'd suggest something like the following

public class Thing
{
    public string name;
    public int amount;
    public string colors;
    //...
}

// Create a dictionary
var dictionary = new Dictionary<string, ICollection<Thing>>();

foreach(Things item in thingsList.jsonfile)
{
    // Get an item with this key
    if (!dictionary.TryGetValue(item.name, out var collection))
    {
        // If it doesn't exist, create a new list
        collection = new List<Thing>();
        dictionary.Add(item.name, collection);
    }
    
    // Add item to the list
    collection.Add(item);
}

This could also be done with a LINQ statement

var dictionary = thingsList.jsonfile.ToDictionary(tl => tl.name, tl)
itsdaniel0
  • 1,059
  • 3
  • 11
  • 28
  • Thank you so much. I think that's what I need to do. I have an additional question. I tried this, and now that I can't add new values in class Thing. I want to add new values, such as location in the class Thing, not to the JSON file. Is it possible to do that? NOTE: I tried to use the TRYGetValue and was not able to because I got a message says read-only. Now I understand that I needed to add IColleciton. Thank you. – 1026 Feb 22 '23 at 19:17
  • If the answer solved your problem, be sure to mark is as accepted. If you have a different question, it's usually best to post a new question. It sounds like you just need to add a new property to Thing class though – itsdaniel0 Feb 22 '23 at 19:20
  • Ask a new question with a new code sample if you need more help. You can reference this question if needed. – itsdaniel0 Feb 22 '23 at 19:53
  • I tried but Stack Over Flow won't let me ask a new question for 7 days (T 3 T) – 1026 Feb 22 '23 at 22:22