如何将列表存储到字典中 [英] How do I store list into dictionary
本文介绍了如何将列表存储到字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello House,
我希望能够将两个列表项添加到字典中,下面是我的代码:
Hello House,
I want to be able to add two list items into dictionary, below is my code:
var attributeValues = new Dictionary<string , string >();
var listAttributeName = new List<string>();
var listAttributeRegion = new List<string>();
var listAttributeIdSplit = new List<string>();
var command = new SqlCommand("select [AttrName], [RegionName] from csowner.CatRegionMap where CatName = 'lands'",connection);
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
listAttributeName.Add(reader["AttrName"].ToString().ToUpper());
listAttributeRegion.Add(reader["RegionName"].ToString());
attributeValues.Add( listAttributeName.Add( reader["AttrName"] ), listAttributeRegion.Add( reader["RegionName"] ) );
}
i尝试将listAttributeName和listAttributeRegion添加到attributeValues,这是例外,请问我能做到这一点吗?
提前谢谢。
i tried adding listAttributeName and listAttributeRegion to attributeValues, it is giving exception, please is there anyway i can do this?
Thanks in advance.
推荐答案
如果您有兴趣将列表项添加到Dictionary,那么,你必须做类似下面的事情
If you are interested in adding the list items to Dictionary then, you have to do something like below
attributeValues.Add( reader["AttrName"].ToString(), reader["RegionName"].ToString() );
由于您的字典是使用键和值声明为字符串,因此您必须在其中添加字符串项。
希望它可以帮到你。
Since your dictionary is declared with key and value as string, you will have to add string items into it.
Hope it helps you.
这篇关于如何将列表存储到字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文