简体   繁体   English

如何获取字典的所有值<TKey, TValue>作为 IList<TValue> ?

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

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.我有一个方法可以让我获得 MyClass 的列表。 I then have another method that converts that list into a dictionary where they key is the id for MyClass.然后我有另一种方法将该列表转换为字典,其中键是 MyClass 的 id。 Later on...and without access to that original list...I'm needing to obtain the original ungrouped list of MyClass.稍后......并且无法访问该原始列表......我需要获得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.现在,我可以理解它已经消失并将 IList 的每个组作为列表的单独元素添加到新列表中......但在这种情况下,它并不是我真正想要的。 I just want a list of all the values in the entire dictionary.我只想要整个字典中所有值的列表。

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

Noticed a lot of answer were quite old.注意到很多答案都很旧。

This will also work:这也将起作用:

using System.Linq;

dict.Values.ToList();

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)。

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();
dictionary.values.toList();

如果你想得到 Sum 就做

myDictionary.values.sum();

Values gets a ICollection containing the values of your dictionary. Values获取包含字典值的ICollection As implied by the definition of your dictionary, it can be defined as a ICollection<IList<MyClass>> collection.正如您的字典定义所暗示的那样,它可以定义为ICollection<IList<MyClass>>集合。 So if you really want a IList<IList<MyClass>> , use spacedog 's solution.因此,如果您真的想要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.请注意,这是非常低效的,您应该再次考虑使用字典来实现您的目标。

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

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