如何插入项目进入一键/值对的对象? [英] How to insert an item into a key/value pair object?

查看:217
本文介绍了如何插入项目进入一键/值对的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧......这里有一个垒球的问题...



我只需要能够在特定位置插入一个键/值对转换成一个对象。我目前正在与一个Hashtable其中,当然,不允许此功能工作。什么是最好的办法。



更新:?另外,我确实需要通过键来查找的能力。



例如...简单化和pseudocoded而是应该传达出点

  //现有的Hashtable 
myHashtable.Add (somekey1,somevalue1);
myHashtable.Add(somekey2,somevalue2);
myHashtable.Add(somekey3,somevalue3);

//一些其他的对象,让我插入一个新的键/值对。
//假设这个对象已经填充了上面的键/值对。
oSomeObject.Insert(newfirstkey,newfirstvalue);



先谢谢了。


解决方案

 列表< KeyValuePair<字符串,字符串>> kvpList =新的List< KeyValuePair<字符串,字符串>>()
{
新KeyValuePair<字符串,字符串>(密钥1,值1),
新KeyValuePair<字符串,字符串>(密钥2,值2),
新KeyValuePair<字符串,字符串>(密钥3,值3),
};

kvpList.Insert(0,新KeyValuePair<字符串,字符串>(新键1,新价值1));



使用此代码:

 的foreach(KeyValuePair<字符串,字符串> KVP在kvpList)
{
Console.WriteLine(的String.Format(键:{0}值:{1},KVP 。重点,kvp.Value);
}

预期输出应该是:

 键:新的密钥1值:新价值1 
键:键1值:值1
键:重点2值:值2
键:关键3价值:价值3

同样会



- 有KeyValuePair或任何其他类型的要使用..



修改工作

要通过键查找,你可以做到以下几点:

  VAR的结果= stringList.Where(S = GT;小号==查找); 

您可以通过执行以下操作,KeyValuePair做到这一点:

  VAR的结果= kvpList.Where(KVP => kvp.Value ==查找); 

最后编辑 -



提出的回答有关KeyValuePair而不是字符串。


Ok...here's a softball question...

I just need to be able to insert a key/value pair into an object at a specific position. I'm currently working with a Hashtable which, of course, doesn't allow for this functionality. What would be the best approach?

UPDATE: Also, I do need the ability to lookup by the key.

For example...oversimplified and pseudocoded but should convey the point

// existing Hashtable
myHashtable.Add("somekey1", "somevalue1");
myHashtable.Add("somekey2", "somevalue2");
myHashtable.Add("somekey3", "somevalue3");

// Some other object that will allow me to insert a new key/value pair.
// Assume that this object has been populated with the above key/value pairs.
oSomeObject.Insert("newfirstkey","newfirstvalue");

Thanks in advance.

解决方案

List<KeyValuePair<string, string>> kvpList = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("Key1", "Value1"),
    new KeyValuePair<string, string>("Key2", "Value2"),
    new KeyValuePair<string, string>("Key3", "Value3"),
};

kvpList.Insert(0, new KeyValuePair<string, string>("New Key 1", "New Value 1"));

Using this code:

foreach (KeyValuePair<string, string> kvp in kvpList)
{
    Console.WriteLine(string.Format("Key: {0} Value: {1}", kvp.Key, kvp.Value);
}

the expected output should be:

Key: New Key 1 Value: New Value 1
Key: Key 1 Value: Value 1
Key: Key 2 Value: Value 2
Key: Key 3 Value: Value 3

The same will work with a KeyValuePair or whatever other type you want to use..

Edit -

To lookup by the key, you can do the following:

var result = stringList.Where(s => s == "Lookup");

You could do this with a KeyValuePair by doing the following:

var result = kvpList.Where (kvp => kvp.Value == "Lookup");

Last edit -

Made the answer specific to KeyValuePair rather than string.

这篇关于如何插入项目进入一键/值对的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆