【发布时间】:2015-08-24 06:50:33
【问题描述】:
我想一一添加值,但在 for 循环中如何迭代 通过一个一个值并将其添加到字典中。
IEnumerable<Customer> items = new Customer[]
{
new Customer { Name = "test1", Id = 111},
new Customer { Name = "test2", Id = 222}
};
我想在i=0 时添加{ Name = "test1", Id = 111}
并想在i=1 n 时添加{ Name = "test2", Id = 222} 等等..
现在我正在为每个键添加完整的集合。(希望使用 foreach 或 forloop 来实现)
public async void Set(IEnumerable collection)
{
RedisDictionary<object,IEnumerable <T>> dictionary = new RedisDictionary>(Settings, typeof(T).Name);
// Add collection to dictionary;
for (int i = 0; i < collection.Count(); i++)
{
await dictionary.Set(new[] { new KeyValuePair<object,IEnumerable <T> ( i ,collection) });
}
}
【问题讨论】:
-
现在不在电脑上,但我想你可以这样 foreach 它: foreach(KeyValuePair
item in dictionary) { // 用 item 做事.Key 和 item.Value } 如果不是编译器会告诉你并称我错了。 -
参数“string Key”是什么意思?
-
@knagaev 抱歉更新了混乱..
-
为什么你的key是object而不是in?
-
你到底用字典做什么? :)
标签: c# dictionary ienumerable ienumerator