简体   繁体   English

如何获取字典中的键列表?

[英]How do I get the list of keys in a Dictionary?

I only want the Keys and not the Values of a Dictionary.我只想要键而不是字典的值。

I haven't been able to get any code to do this yet.我还没有能够得到任何代码来做到这一点。 Using another array proved to be too much work as I use remove also.使用另一个数组被证明是太多的工作,因为我也使用 remove 。

How do I get a List of the Keys in a Dictionary?如何获取字典中的键列表?

List<string> keyList = new List<string>(this.yourDictionary.Keys);

You should be able to just look at .Keys :你应该能够只看.Keys

    Dictionary<string, int> data = new Dictionary<string, int>();
    data.Add("abc", 123);
    data.Add("def", 456);
    foreach (string key in data.Keys)
    {
        Console.WriteLine(key);
    }

To get list of all keys获取所有键的列表

using System.Linq;
List<String> myKeys = myDict.Keys.ToList();

System.Linq is supported in .Net framework 3.5 or above. .Net framework 3.5 或更高版本支持 System.Linq。 See the below links if you face any issue in using System.Linq如果您在使用 System.Linq 时遇到任何问题,请参阅以下链接

Visual Studio Does not recognize System.Linq Visual Studio 无法识别 System.Linq

System.Linq Namespace System.Linq 命名空间

Marc Gravell's answer should work for you. Marc Gravell 的回答应该对你有用。 myDictionary.Keys returns an object that implements ICollection<TKey> , IEnumerable<TKey> and their non-generic counterparts. myDictionary.Keys返回一个实现ICollection<TKey>IEnumerable<TKey>及其非通用对应物的对象。

I just wanted to add that if you plan on accessing the value as well, you could loop through the dictionary like this (modified example):我只想补充一点,如果您还计划访问该值,则可以像这样遍历字典(修改后的示例):

Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);

foreach (KeyValuePair<string, int> item in data)
{
    Console.WriteLine(item.Key + ": " + item.Value);
}

I can't believe all these convoluted answers.我无法相信所有这些令人费解的答案。 Assuming the key is of type: string (or use 'var' if you're a lazy developer): -假设密钥的类型为:string(如果您是一个懒惰的开发人员,则使用“var”):-

List<string> listOfKeys = theCollection.Keys.ToList();

The question is a little tricky to understand but I'm guessing that the problem is that you're trying to remove elements from the Dictionary while you iterate over the keys.这个问题有点难以理解,但我猜测问题在于您在迭代键时试图从 Dictionary 中删除元素。 I think in that case you have no choice but to use a second array.我认为在这种情况下,您别无选择,只能使用第二个数组。

ArrayList lList = new ArrayList(lDict.Keys);
foreach (object lKey in lList)
{
  if (<your condition here>)
  {
    lDict.Remove(lKey);
  }
}

If you can use generic lists and dictionaries instead of an ArrayList then I would, however the above should just work.如果您可以使用通用列表和字典而不是 ArrayList,那么我会,但是以上应该可以正常工作。

Or like this:或者像这样:

List< KeyValuePair< string, int > > theList =
    new List< KeyValuePair< string,int > >(this.yourDictionary);

for ( int i = 0; i < theList.Count; i++)
{ 
  // the key
  Console.WriteLine(theList[i].Key);
}

For a hybrid dictionary, I use this:对于混合字典,我使用这个:

List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());

I often used this to get the key and value inside a dictionary: (VB.Net)我经常用它来获取字典中的键和值:(VB.Net)

 For Each kv As KeyValuePair(Of String, Integer) In layerList

 Next

(layerList is of type Dictionary(Of String, Integer)) (layerList 的类型是 Dictionary(Of String, Integer))

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

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