c# - 如何在不循环的情况下使用“列表到字典”添加项目?

标签 c# dictionary asp.net-4.0

我有这本字典-

IDictionary<DateTime, int> kamptslist = new Dictionary<DateTime, int>();
List<int> listints= GetListofints(); //for values
List<int> listdates= GetListofdates();// for keys


我可以以某种方式直接将列表分配给字典,而不是实际执行foreach并一次添加一项吗?

最佳答案

使用Enumerable.Zip将两个序列压缩在一起,然后使用Enumerable.ToDictionary

var kamptslist = listdates.Zip(listints, (d, n) => Tuple.Create(d, n))
                          .ToDictionary(x => x.Item1, x => x.Item2);

关于c# - 如何在不循环的情况下使用“列表到字典”添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880301/

相关文章:

c# - 这是如何定义的?

c# - 调用对象的 ContainsKey 后字典抛出 StackOverflowException

c# - 从后面的代码添加用户控件

c# - 将 lambda 表达式转换为派生类型

c# - LInq 查询集合中的集合

dictionary - 如何在awk中清除 map ?

asp.net-4.0 - ASP.NET 中的购物车设计

javascript - 交替/双模式 JavaScript 水印

c# - 是否可以发布从 .net 框架获取并在线修改的代码?

c++ - 一次打印多个相等键的值 C++