简体   繁体   English

从字典中检索值的最快方法

[英]Fastest way to retrieve values from dictionary

I have a var myDictionary = new Dictionary<int, string> with the following data: 我有一个带有以下数据的var myDictionary = new Dictionary<int, string>

123, "Do this"
234, "Do that"
345, "Do something"
123, "Do that"
567, "Do anything"
234, "Do something"

What's the best way for me to retrieve only the values for any given key? 对我来说,仅检索任何给定键的值的最佳方法是什么? Say, I want to get only the values for 123. 说,我只想获取123的值。

If you want to have multiple different values grouped under one key you probably have to change the structure of your dictionary to something like this: 如果要将多个不同的值归为一个键,则可能必须将字典的结构更改为以下形式:

var myDictionary = new Dictionary<int, List<string>>();

You then initialize the list for each new key or if the key's already there you add the item to the list. 然后,您为每个新密钥初始化列表,或者如果该密钥已经存在,则将项目添加到列表中。

if (!myDictionary.ContainsKey(myKey))
{
    myDictionary[myKey] = new List<string();
}
myDictionary[myKey].Add(myItem);

And you get the items in a standard way: 您将以标准方式获得商品:

if (myDictionary.ContainsKey(myKey))
{
    var results = myDictionary[myKey];
}

This will give you a list that you can then query to see what items have been returned. 这将为您提供一个列表,您可以随后查询以查看返回了哪些项目。

A Dictionary object cannot have multiple items with the same key. 字典对象不能有多个具有相同键的项。 Instead, you want to use KeyValuePair . 相反,您想使用KeyValuePair

The code might look something like this: 该代码可能看起来像这样:

var items = new List<KeyValuePair<int, String>>();

items.Add(new KeyValuePair<int, String>(123, "Do this"));
items.Add(new KeyValuePair<int, String>(234, "Do that"));
items.Add(new KeyValuePair<int, String>(345, "Do something"));
items.Add(new KeyValuePair<int, String>(123, "Do that"));
items.Add(new KeyValuePair<int, String>(567, "Do anything"));
items.Add(new KeyValuePair<int, String>(234, "Do something"));

// This is the key you are looking for
int desiredValue = 123;

foreach (var v in items.Where(kvp => kvp.Key == desiredValue))
{
    // Access the values you need here
    Console.WriteLine(v.Value);
}

Where the output would be: 输出将是:

Do this 
Do that

You can see this example in action here . 您可以在此处看到此示例的实际操作。 Happy coding :) 快乐的编码:)

See code below 见下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>() {
               {123, new List<string>() {"Do this", "Do that"}},
               {234, new List<string>() {"Do that", "Do something"}},
               {345, new List<string>() {"Do something"}},
               {567, new List<string>() {"Do anything"}}
            };

            List<string> results = dict[123];

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM