开发者

how to add duplicate keys to dictionary<string,string>

how to add duplicate keys t开发者_如何学Pythono dictionary

i.e i have already added the key,value pair as rollno,1 but i need to add the same parameter to the dictionary,but it is not allowing to add. how to add duplicated keys/repeated key in dictionary .

or any other choice.. any idea???


That doesn't make sense, if you added duplicate keys to the dictionary, how would it be able to find which one you want when you look them up?

Possibly you're looking for something like a List< KeyValuePair < T, T > >? Where you could store a list of pairs rather than an actual dictionary.


Check out this:

What is the point of Lookup<TKey, TElement>?

You can use the Lookup class to help you create collections with duplicates keys.


I think it should work in your case

class Program
    {
         private static List<KeyValuePair<string, int>> d = new List<KeyValuePair<string, int>>();

        static void Main(string[] args)
        {
             d.Add(new KeyValuePair<string, int>("rollno", 1));
             d.Add(new KeyValuePair<string, int>("rollno", 2));
             d.Add(new KeyValuePair<string, int>("rollno", 3));
             var result = d.Where(x => x.Key == "joe");
            foreach(var q in result)
                Console.WriteLine(q.Value   );
            Console.ReadLine();
        }
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜