我想逐一添加值,但在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
时,我想添加{ Name = "test2", Id = 222}
,以此类推。
现在,我正在每个键中添加完整的集合。(希望使用foreach或for循环实现这一点)
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) });
}
}
IEnumerable
,它本身没有索引器呢?看起来你想通过索引访问值? - Yuval Itzchakov