Question Multivalued dictionary

jkeertir

Member
Joined
Apr 18, 2020
Messages
9
Programming Experience
1-3
Dear All,
I want to create a dictionary whose key is string and values are string and list of six variables.


How can I do this in c#?
 
If you want values that contain a String and six other values then you need a type that has properties for a String and six other values. Define that type, maybe as a structure but most likely as a class, and then specify that as the type of the values in the Dictionary.
 
Hello and welcome to the forums. You caught me on a good day, so try something like this :

C#:
            Dictionary<string, Tuple<string, List<string>>> kvp = new Dictionary<string, Tuple<string, List<string>>>();
            string key = "key_Value";
            string a_Value = "whatever";
            List<string> ListOf_String = new List<string>();
            string[] strArr = { "String_A", "String_B", "String_C" };
            ListOf_String.AddRange(strArr);
            Tuple<string, List<string>> tuple = new Tuple<string, List<string>>(a_Value, ListOf_String);
            kvp.Add(key, tuple);
            foreach (KeyValuePair<string, Tuple<string, List<string>>> keyValue in kvp)
            {
                //Do something with your nested items.
            }
This tidyer version might be easier to read :
C#:
            Dictionary<string, Tuple<string, List<string>>> kvp = new Dictionary<string, Tuple<string, List<string>>>();
            List<string> ListOf_String = new List<string>();
            ListOf_String.AddRange(new string[] { "String_A", "String_B", "String_C" });
            kvp.Add("key_Value", new Tuple<string, List<string>>("whatever", ListOf_String));
            foreach (KeyValuePair<string, Tuple<string, List<string>>> keyValue in kvp)
            {
                //Do something with your nested items.
            }
All of the documentation for these types can be found in my signature.

According to what you asked, that code above does exactly what you stated. Hope it helps. (y)
 
Just to add, you can also use string[] vars = { "s1", "s2", "s3", "s4", "s5", "s6" }; in place of using Tuples. And since I just noticed you have values and not value. In which case, if you have multiple value(s), you will need to overload the Tuple ie :

Dictionary<string, Tuple<string, string, List<string>>> kvp = new Dictionary<string, Tuple<string, string, List<string>>>();
OR use a string array instead of overloading the Tuple with multiple string.
Dictionary<string, Tuple<string[], List<string>>> kvp = new Dictionary<string, Tuple<string[], List<string>>>();

You'll also need to update the code accordingly to include those changes. ;)
 
Dear All,

I have a dict
<string,tuple{string,int,list<string>}>

around 12 members in dict.
I need to group 6 members in a group,so two groups are created
these two groups data needs to be dump into excel file.

So should i go for singleton class or interface for writing a method for dumping the list in excel.
 
Last edited:
Why do you think either would be useful? A singleton is specifically so there can never be more than one instance of your type. Is there a reason to impose that restriction in this case? There are various reasons to use interfaces, including for testability. Are you planning on writing unit tests for this type? If not, do you see any other benefits to defining an interface? You'll be implementing instance members in a class in either case so you would need some benefit to one or the other to implement either.
 
Why do you think either would be useful? A singleton is specifically so there can never be more than one instance of your type. Is there a reason to impose that restriction in this case? There are various reasons to use interfaces, including for testability. Are you planning on writing unit tests for this type? If not, do you see any other benefits to defining an interface? You'll be implementing instance members in a class in either case so you would need some benefit to one or the other to implement either.
No we don't have any specific reason .I just want to structure my code
 
Then I would suggest neither. Implementing a singleton is not something to do for no reason. There can be numerous advantages to defining interfaces and implementing them in your classes but it's just extra work if you're not going to take advantage of any of them. If you want to future-proof your code then you can certainly implement an interface but you then need to make sure that you use it as you should, otherwise there was no point. If you have to go back and refactor your code to use the interface later, you may as well have waited until then to define the interface at all.
 
Dear All,
I have a dictionary having tuple .I have this
Dictionary<string, Tuple<string,int, List<string>>>

how can i access tuple values?
 
Dictionaries only work one way: you provide the key and it gives you the corresponding value. I'm not sure why you created a Dictionary in the first place if you didn't expect that to be the case. E.g.
C#:
var myDictionary = new Dictionary<string, Tuple<string, int, List<string>>>();

// ...

Console.WriteLine("Enter key:");

var key = Console.ReadLine();

if (!myDictionary.ContainsKey(key))
{
    Console.WriteLine("Invalid key.");
}
else
{
    var value = myDictionary[key];

    Console.WriteLine("Value: {0}, {1}, {2}", value.Item1, value.Item2, string.Join("|", value.Item3));
}
or:
C#:
var myDictionary = new Dictionary<string, Tuple<string, int, List<string>>>();

// ...

Console.WriteLine("Enter key:");

var key = Console.ReadLine();

if (myDictionary.TryGetValue(key, out var value))
{
    Console.WriteLine("Value: {0}, {1}, {2}", value.Item1, value.Item2, string.Join("|", value.Item3));
}
else
{
    Console.WriteLine("Invalid key.");
}
 
Topic should be merged with Question - Multivalued dictionary - as its the same and its the origins of this topic.

That's where he got the code from.

C#:
            Dictionary<string, Tuple<string, int, List<string>>> myDictionary = new Dictionary<string, Tuple<string, int, List<string>>>();
            myDictionary.Add("string A", new Tuple<string, int, List<string>>("string B", 1, new List<string> { "String C" }));
            if (myDictionary.TryGetValue("string A", out var value))
            {
                Debug.WriteLine(string.Format("{0}, {1}, {2}", value.Item1, value.Item2, value.Item3[0]));
            }
This outputs :
string B, 1, String C

Debug.WriteLine depends on the using directive for using System.Diagnostics;. String Format converts the values of the objects into strings and essentially makes them into another string. value.Item1; where value is the tuple in your dictionary and .Item1 if the first value, value.Item2 is your int and value.Item3 is your List<string> and its value is accessed but calling the first position in the list<string> by value.Item3[0] from the tuple's third item.

For easier understanding. An explicit version of line 3 would look like this :
C#:
            if (myDictionary.TryGetValue("string A", out Tuple<string, int, List<string>> value))
 
I agree this should be merged.

Tuples are great for quick transient data. it is looking like in the particular user's case, the data hangs around for a while, and he actually needs to do multiple manipulations. Furthermore, from his other thread (design approach), it seems like he knows that the dictionary will only have 12 entries.

I would have recommended something like:
C#:
class Foo
{
    string Bar;
    int Baz;
    List<string> Bizz;
}

Dictionary<string, Foo> myData;
 
I have a dictionary<string,Tuple<string,int,List<classmembers>>>mydict

When I am creating a dictionary like
mydict.Add("aaa",Tuple<"a",1,List<object>>)
suppose the length of the list in tuple is 10
mydict.Add("bbb",Tuple<"a",1,List<object>>)"}
suppose the length of the list in tuple is 10

after the dictionary is completely filled it gives me a map as Item3 count is 20(10 of first list and 10 of second entry in the dict for each entry in dictionary.
 
Without you showing us real code as to how you are inserting objects and later getting the count of objects, all we can do is speculate. For example right now I'm speculating that you are simply recycling the same list of objects that you are adding as part of the tuple.
 

Latest posts

Back
Top Bottom