[英]C# - Dictionary<key, value> to List<T>
我想將我的Dictionary<int, string>
映射到List<Customer>
,其中Customer
具有兩個屬性Id
和Name
。 現在,我想將字典的整數Key
映射到List<Customer>[i].Key
屬性,並將字典的Value
映射到List<Customer>[i].Name
。迭代地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();
給定myDictionary
,並且myList
成為目標列表:
myDictionary.ToList()
.ForEach(x =>
myList.Add( new Customer() {Id = x.Key, Name = x.Value} )
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.