簡體   English   中英

C#-字典 <key, value> 列出 <T>

[英]C# - Dictionary<key, value> to List<T>

我想將我的Dictionary<int, string>映射到List<Customer> ,其中Customer具有兩個屬性IdName 現在,我想將字典的整數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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM