如何获取 Dictionary 的所有值? 作为 IList

发布于 2024-07-13 22:24:45 字数 729 浏览 6 评论 0 原文

我有一个以下字典:

IDictionary<int, IList<MyClass>> myDictionary 

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


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

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


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

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

System.Collections.Generic.IList<MyClass>

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

那么,在不循环字典中的每个键值并创建我想要的列表的情况下,我怎样才能得到我想要的东西呢?

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....


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.


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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光与爱终年不遇 2024-07-20 22:24:45

注意到很多答案都很旧了。

这也将起作用:

using System.Linq;

dict.Values.ToList();

Noticed a lot of answer were quite old.

This will also work:

using System.Linq;

dict.Values.ToList();
君勿笑 2024-07-20 22:24:45

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

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

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

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

君勿笑 2024-07-20 22:24:45

John 建议的一个变体:

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

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

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

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:

var flattenedList = dict.Values.SelectMany(x => x).ToList();
苍白女子 2024-07-20 22:24:45
dictionary.values.toList();

如果你想得到 Sum 就这样做

myDictionary.values.sum();
dictionary.values.toList();

if You want to get Sum just do

myDictionary.values.sum();
世界如花海般美丽 2024-07-20 22:24:45

Values 获取 ICollection 包含字典的值。 正如字典定义所暗示的,它可以定义为 ICollection> 集合。 因此,如果您确实想要一个 IList>,请使用 spacedog 的解决方案。

如果您真正想要的是一个平面“IList”,那么除了循环遍历每个值之外没有其他解决方案:

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

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

Values gets a ICollection containing 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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文