Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populate a dictionary using linq

I have the following empty Dictionary

Dictionary<Guid, string> appTypeMap = new Dictionary<Guid, string>(); 

and the following list:

List<ApplicationType> allApplicationTypes = _applicationTypeService.GetAll() 

Application type is described as follows:

public class ApplicationType {    public Guid id {get; set;}    public String name {get; set;} } 

I want to populate the dictionary using LINQ.
How do I do that? Thanks.

like image 642
Bick Avatar asked Mar 10 '13 16:03

Bick


Video Answer


1 Answers

appTypeMap = allApplicationTypes.ToDictionary(x => x.id, x => x.name); 

However, it will create a new dictionary, not fill yours.

like image 70
MarcinJuraszek Avatar answered Sep 20 '22 17:09

MarcinJuraszek