c# – 如何使用for each或foreach迭代IEnumerable集合?
发布时间:2020-12-15 22:18:59 所属栏目:百科 来源:网络整理
导读:我想逐个添加值但在for循环中如何迭代 通过逐个值并将其添加到字典中. IEnumerableCustomer items = new Customer[] { new Customer { Name = "test1",Id = 111},new Customer { Name = "test2",Id = 222} }; 我想在i = 0时添加{Name =“test1”,Id = 111}
我想逐个添加值但在for循环中如何迭代
通过逐个值并将其添加到字典中. IEnumerable<Customer> items = new Customer[] { new Customer { Name = "test1",Id = 111},new Customer { Name = "test2",Id = 222} }; 我想在i = 0时添加{Name =“test1”,Id = 111} 现在我正在每个键中添加完整的集合.(希望使用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) }); } } 解决方法
新版本
var dictionary = items.Zip(Enumerable.Range(1,int.MaxValue - 1),(o,i) => new { Index = i,Customer = (object)o }); 顺便说一句,字典是一些变量的坏名字. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读