Question

Je veux mon carte Dictionary<int, string> à un List<Customer>Customer a deux propriétés Id et Name. Maintenant, je veux carte mon entier Key du dictionnaire à la propriété List<Customer>[i].Key et Value du dictionnaire à List<Customer>[i].Name itérativement.

Besoin d'aide pour le même.

Était-ce utile?

La solution

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();

You can also use an appropriate Customer constructor (if available) instead of the example property setter syntax.

Autres conseils

You could do something like:

 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();

Given myDictionary is populated and myList ist the target list:

myDictionary.ToList()
            .ForEach(x => 
                     myList.Add( new Customer() {Id = x.Key, Name = x.Value} )
                    );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top