[英]How to convert a IEnumerable to Dictionary
I have dictionary like this 我有这样的字典
Dictionary<MyStructure, MyObject>
MyStructure means public struct ... MyObject means a specific entity ...
MyStructure表示公共结构... MyObject表示特定实体...
Then through events I pass my dictionary as an 然后通过事件我将字典作为
IEnumerable<MyObject> col = this.GetCollection().Values;
Then when I manage my event I use my IEnumerable but I want to convert it to a former dictionary to access its properties but I'm not able to do it. 然后,当我管理事件时,我使用IEnumerable,但我想将其转换为以前的字典以访问其属性,但我无法执行。 I try this.
我尝试这个。
Dictionary<MyStructure, MyObject> dict =
args.MyObjectCollection.ToDictionary(x => x.Key, y => y.Value);
I'm using "using System.Linq;" 我正在使用“使用System.Linq;” and Key and Value are not recognize.
和键和值无法识别。 When I write point after x or y.
当我在x或y之后写点时。 The helper shows me the properties of the object MyObject.
帮手向我展示了对象MyObject的属性。
when you make your IEnumerable, you loose your MyStructure Keys ... If you know a way to create the appropriate structure from the MyObject instances, then you can solve this ... 当使IEnumerable时,您将松开MyStructure键...如果您知道一种从MyObject实例创建适当结构的方法,则可以解决此问题...
MyObjectCollection.ToDictionary(x=>makeStructFromMyObject(x), x=>x);
//... with...
private MyStruct makeStructFromMyObject(MyObject obj)
{
//to be implemented by you
}
if you are looking for an alternative ... pass the whole dictionary from the point where it still has the original data 如果您正在寻找替代方案...从整个字典中保留原始数据
If your collection is not a collection of KeyValuePairs or some other type with Key and Value properties, you cannot call them. 如果您的集合不是KeyValuePairs或具有Key和Value属性的其他类型的集合,则不能调用它们。 A collection of all the values is simply a collection of that type.
所有值的集合就是该类型的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.