C# 如何将 Dictionary<TKey, TValue> 的所有值作为 IList<TValue> 获取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/555750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:01:39  来源:igfitidea点击:

How do I get all the values of a Dictionary<TKey, TValue> as an IList<TValue>?

c#.net-3.5dictionaryilist

提问by mezoid

I have a the following dictionary:

我有以下字典:

IDictionary<int, IList<MyClass>> myDictionary 

and I am wanting to get all the values in the dictionary as an IList....

我想将字典中的所有值作为 IList 获取....



Just to add a bit of a background as to how I've gotten into this situation....

只是为了添加一些关于我如何陷入这种情况的背景......

I have a method that gets me a list of MyClass. I then have another method that converts that list into a dictionary where they key is the id for MyClass. Later on...and without access to that original list...I'm needing to obtain the original ungrouped list of MyClass.

我有一个方法可以让我获得 MyClass 的列表。然后我有另一种方法将该列表转换为字典,其中键是 MyClass 的 id。稍后......并且无法访问该原始列表......我需要获得MyClass的原始未分组列表。



When I pass myDictionary.Values.ToList() to a method that takes an IList I get a compile error that says that it can't convert from

当我将 myDictionary.Values.ToList() 传递给采用 IList 的方法时,我收到一个编译错误,指出它无法从

System.Collections.Generic.List<System.Collections.Generic.IList<MyClass>> 

to:

到:

System.Collections.Generic.IList<MyClass>

Now, I can understand that its gone and added each of the groups of IList to the new list as separate elements of the list....but in this instance its not really what I'm after. I just want a list of all the values in the entire dictionary.

现在,我可以理解它已经消失并将 IList 的每个组作为列表的单独元素添加到新列表中......但在这种情况下,它并不是我真正想要的。我只想要整个字典中所有值的列表。

How then can I get what I'm after without looping through each of the key values in the dictionary and creating the list I want?

那么如何在不遍历字典中的每个键值并创建我想要的列表的情况下获得我想要的内容?

采纳答案by John Leidegren

Because of how a dictionary (or hash table) is maintained this is what you would do. Internally the implementation contains keys, buckets (for collision handling) and values. You might be able to retrieve the internal value list but you're better of with something like this:

由于字典(或哈希表)的维护方式,这就是您要做的。在内部,实现包含键、桶(用于冲突处理)和值。您可能能够检索内部值列表,但您最好使用以下内容:

IDictionary<int, IList<MyClass>> dict;
var flattenList = dict.SelectMany( x => x.Value );

It should do the trick ;) SelectMany flattens the result which means that every list gets concatenated into one long sequence (IEnumerable`1).

它应该可以解决问题;) SelectMany 将结果展平,这意味着每个列表都被连接成一个长序列 (IEnumerable`1)。

回答by Mac

Valuesgets a ICollectioncontaining the values of your dictionary. As implied by the definition of your dictionary, it can be defined as a ICollection<IList<MyClass>>collection. So if you really want a IList<IList<MyClass>>, use spacedog's solution.

Values得到一个ICollection包含你的字典的值。正如您的字典定义所暗示的那样,它可以定义为一个ICollection<IList<MyClass>>集合。因此,如果您真的想要一个IList<IList<MyClass>>,请使用spacedog的解决方案。

If what you really want is a flat `IList', then there is no other solution than looping through each value :

如果您真正想要的是一个扁平的“IList”,那么除了遍历每个值之外别无他法:

IList<MyClass> l=new List<MyClass>();
foreach (IList<MyClass> v in myDictionary.Values)
    l.AddRange(v);

Note that this is so grossly inefficient that you should think again about using a dictionary for what you are trying to achieve.

请注意,这是非常低效的,您应该再次考虑使用字典来实现您的目标。

回答by Jon Skeet

A variation on John's suggestion:

约翰建议的变体:

var flattenedValues = dict.Values.SelectMany(x => x);

If you need them in a list, you can of course call ToList:

如果您在列表中需要它们,您当然可以调用 ToList:

var flattenedList = dict.Values.SelectMany(x => x).ToList();

回答by MrProgram

Noticed a lot of answer were quite old.

注意到很多答案都很旧。

This will also work:

这也将起作用:

using System.Linq;

dict.Values.ToList();

回答by R.ali

dictionary.values.toList(); if You want to get Sum just do myDictionary.values.sum(); Thanks

字典.values.toList(); 如果你想得到 Sum 就做 myDictionary.values.sum(); 谢谢