0

我想将 my 映射到Dictionary<int, string>具有两个属性和. 现在我想将字典的整数映射到属性,并将字典的整数映射到迭代。List<Customer>CustomerIdNameKeyList<Customer>[i].KeyValueList<Customer>[i].Name

同样需要帮助。

4

4 回答 4

11
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构造函数(如果可用)而不是示例属性设置器语法。

于 2011-03-21T16:26:06.537 回答
3

您可以执行以下操作:

 List<Customer> list = theDictionary
                         .Select(e => new Customer { Id = e.Key, Name = e.Value })
                         .ToList();
于 2011-03-21T16:26:20.513 回答
2
var myList = (from d in myDictionary
             select new Customer {
               Key = d.Key,
               Name = d.Value
             }).ToList();
于 2011-03-21T16:27:10.050 回答
0

GivenmyDictionary已填充并且myList是目标列表:

myDictionary.ToList()
            .ForEach(x => 
                     myList.Add( new Customer() {Id = x.Key, Name = x.Value} )
                    );
于 2011-03-21T16:32:42.017 回答