当索引不可用时,如何访问IDictionary对象中的KeyValuePair?

编程入门 行业动态 更新时间:2024-10-27 10:22:55
本文介绍了当索引不可用时,如何访问IDictionary对象中的KeyValuePair?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好,

我在尝试访问元素时有一个要求,即存储在IDictionary对象中的KeyValuePair对象,但问题是它们的顺序是可更新的.我想将KeyValuePair对象中的每个值"存储在不同的类对象中 为此我创建的.可以说我正在做一些映射工作.但是唯一的问题是所有元素的输入顺序都可以更新.他们的钥匙"也可以更新.

I have a requirement where I am trying to access the elements i.e. KeyValuePair objects stored in a IDictionary object but the problem is their order is updatable. I want to store each of the 'Value' from the KeyValuePair object in a different class object which I created for this purpose. We can say that I am doing some mapping kind of stuff. But the only problem is the input order of all the elements can be updated. Also their 'Key' can be updated.

如何提取此类信息?

请帮助,在此先谢谢

这里有一个示例与我正在尝试做的事情有关.

Here is a sample relates what I am trying to do.

Dictionary<object, object> Author = new Dictionary<object, object>(); Author.Add("Name", "John Smith"); //Later it "Name" can be "FullName" just for an example Author.Add("Age", 25); Author.Add("DOB", "1996-08-06"); // I am trying to extract this info in my Author class as AuthorClass author1 = new AuthorClass(); author1.Name = (string)Author.ElementAt(0).Value; author1.Age = Convert.ToInt32(Author.ElementAt(1).Value); author1.DOB = Convert.ToDateTime(Author.ElementAt(2).Value); // The AuthorClass Class class AuthorClass { public string Name { get; set; } public int Age { get; set; } public DateTime DOB { get; set; } }

此处,当作者"元素的顺序更改时,代码将失败.请帮助解决此问题.

Here when the order of 'Author' elements changes, the code will fail. Please help to resolve this.

推荐答案

嗨!

使用键名代替"ElementAt(index)".

Use the name of the key instead of "ElementAt(index)".

例如:

AuthorClass author1 = new AuthorClass(); author1.Name = (string)Author["Name"]; author1.Age = Convert.ToInt32(Author["Age"]);

如果它对您有好处,您可以将此答案标记为您的问题的答案吗?

更多推荐

当索引不可用时,如何访问IDictionary对象中的KeyValuePair?

本文发布于:2023-11-12 19:25:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中   索引   IDictionary   KeyValuePair

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!