我有一个以下字典:
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?
发布评论
评论(5)
注意到很多答案都很旧了。
这也将起作用:
Noticed a lot of answer were quite old.
This will also work:
由于字典(或哈希表)的维护方式,这就是您要做的。 在内部,该实现包含键、桶(用于冲突处理)和值。 您也许能够检索内部值列表,但最好使用这样的东西:
它应该可以解决问题;) 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:
It should do the trick ;) SelectMany flattens the result which means that every list gets concatenated into one long sequence (IEnumerable`1).
John 建议的一个变体:
如果您需要将它们放在列表中,您当然可以调用 ToList:
A variation on John's suggestion:
If you need them in a list, you can of course call ToList:
如果你想得到 Sum 就这样做
if You want to get Sum just do
Values
获取ICollection
包含字典的值。 正如字典定义所暗示的,它可以定义为ICollection>
集合。 因此,如果您确实想要一个IList>
,请使用 spacedog 的解决方案。如果您真正想要的是一个平面“IList”,那么除了循环遍历每个值之外没有其他解决方案:
请注意,这效率非常低,您应该再次考虑使用字典来实现您想要实现的目标。
Values
gets aICollection
containing the values of your dictionary. As implied by the definition of your dictionary, it can be defined as aICollection<IList<MyClass>>
collection. So if you really want aIList<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 :
Note that this is so grossly inefficient that you should think again about using a dictionary for what you are trying to achieve.