-1

I currently have a dictionary that loads up with data on the start of the program and its something like:

  Dictionary<string, List<KeyValuePair<string,string>>> variants =
    new Dictionary<string, List<KeyValuePair<string, string>>>();

How would i reference it if i want to access the Keyvaluepair list of a specific string...

here's an understanding of it: each list has an identifier (which is the string) --> the string leads to a list of Keyvaluepairs, and i want to chose them randomly

Ramie
  • 1,171
  • 2
  • 16
  • 35
  • How about simply [reading the MSDN documentation for `Dictionary<>`](http://msdn.microsoft.com/en-us/library/xfhwa508.aspx)? – Uwe Keim Feb 05 '13 at 19:47
  • Also, why not make it a `Dictionary>`? – TylerOhlsen Feb 05 '13 at 19:48
  • @TylerOhlsen in a `Dictionary` the `key` needs to be unique, while in a `KeyValuePair` you can have the same key over and over... – balexandre Feb 05 '13 at 19:50
  • @uwekeim im not familiar with the terminology. Id rather have someone explain it to me here, much better and easy to understand. i'm an amateur in programming – Ramie Feb 05 '13 at 19:55
  • possible duplicate of [What is the best way to iterate over a Dictionary in C#?](http://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c) – Ramie Jan 21 '15 at 14:36

4 Answers4

3

Let's break it down:

// This is your dictionary.
Dictionary<string, List<KeyValuePair<string,string>>> variants = new Dictionary<string, List<KeyValuePair<string, string>>>();

// Let's get the list of variants for a given key.
string key = "myKey";
List<KeyValuePair<string,string>> variantsForKey = variants[key];

// If we wanted to get the first variant, we'd use one of these:
KeyValuePair<string,string> firstVariant = variantsForKey[0];
firstVariant = variantsForKey.First(); // Using Linq for fun and profit!

// Now, we want a random variant? No problem!
Random rand = new Random(); // Remember to initialize this only once!
int listLength = variantsForKey.Count;
int randomVariantIndex = rand.Next(0, listLength );
KeyValuePair<string,string> randomVariant = variantsForKey[randomVariantIndex];

Voila!

Now, what you might want to do is wrap those last 3 lines in an extension method:

public static class VariantExtensions
{
    private static Random rand = new Random();
    public static KeyValuePair<string,string> GetRandomVariant(this List<KeyValuePair<string,string>> variantsForKey)
    {
         int listLength = variantsForKey.Count;
         int randomVariantIndex = rand.Next(0, listLength );
         return variantsForKey[randomVariantIndex];
    }
}

and now you can use this:

Dictionary<string, List<KeyValuePair<string,string>>> variants = ...;
string key = "myKey";
KeyValuePair<string,string> randomVariant = variants[key].GetRandomVariant();
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • Seems fine, You should use `int highestIndexInList = variantsForKey.Count();` though, because the second argument of the `Random.Next(int,int)` method is an **exclusive** upper bound... – Grx70 Feb 05 '13 at 20:01
  • Hey, you're right! I think I might have introduced some subtle bugs at several points in my life, with the last items in a list never getting chosen. Thanks. :) – Avner Shahar-Kashtan Feb 05 '13 at 20:02
1

You use the [] operator.

to put the value into the hash table,

like this: variants[key] = valueHere;

and to get the value from the hashtable,

var valueHere = variants[key];

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

It's hard to understand what exactly you are after. Does this help?

using System;
using System.Collections.Generic;

namespace SO14715593
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, List<KeyValuePair<string, string>>> variants = new Dictionary<string, List<KeyValuePair<string, string>>>();
            variants["test"] = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("key1", "item1"),
                    new KeyValuePair<string, string>("key2", "item2"),
                    new KeyValuePair<string, string>("key3", "item3")
                };

            Random random = new Random();
            int index = random.Next(variants["test"].Count);
            Console.WriteLine("{0};{1}", variants["test"][index].Key, variants["test"][index].Value);
        }
    }
}
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
0

you may use something like:

var list = variants[stringToGetCorrespondingList]

armadillo.mx
  • 934
  • 1
  • 11
  • 17