我想将 my 映射到Dictionary<int, string>
具有两个属性和. 现在我想将字典的整数映射到属性,并将字典的整数映射到迭代。List<Customer>
Customer
Id
Name
Key
List<Customer>[i].Key
Value
List<Customer>[i].Name
同样需要帮助。
我想将 my 映射到Dictionary<int, string>
具有两个属性和. 现在我想将字典的整数映射到属性,并将字典的整数映射到迭代。List<Customer>
Customer
Id
Name
Key
List<Customer>[i].Key
Value
List<Customer>[i].Name
同样需要帮助。
var dict = new Dictionary<int, string>(); // populate this with your data
var list = dict.Select(pair => new Customer { Id = pair.Key, Name = pair.Value }).ToList();
您还可以使用适当的Customer
构造函数(如果可用)而不是示例属性设置器语法。
您可以执行以下操作:
List<Customer> list = theDictionary
.Select(e => new Customer { Id = e.Key, Name = e.Value })
.ToList();
var myList = (from d in myDictionary
select new Customer {
Key = d.Key,
Name = d.Value
}).ToList();
GivenmyDictionary
已填充并且myList
是目标列表:
myDictionary.ToList()
.ForEach(x =>
myList.Add( new Customer() {Id = x.Key, Name = x.Value} )
);