forum

Home / DeveloperSection / Forums / Populate Concurrent Dictionary from Dictionary

Populate Concurrent Dictionary from Dictionary

Anonymous User 2588 18-Dec-2013

How can I populate the data from Dictionary to Concurrent Dictionary.

I have following,

public class Employee
{
    public int Id {get; set; }
    public string Name{ get; set; }
}
var names = new List<Employee>
{
    new Employee { Id= 1, Name = "Name1" },
    new Employee { Id= 1, Name = "Name1" },
    new Employee { Id= 2, Name = "Name2" },
    new Employee { Id= 3, Name = "Name3" },
};

And I populate to Dictionary like

Dictionary<int, List<Employee>> dict = names.GroupBy(n => n.Id).ToDictionary(g => g.Key, g => g.ToList());

and I want to create,

ConcurrentDictionary<int, List<Employee>> concDict

and I tried,

ConcurrentDictionary<int, List<Employee>> concDict = new ConcurrentDictionary<int, List<Employee>>();

dict.ToList().ForEach(e => concDict.TryAdd(e.Key, e.Value));

Is there any built-in extension method like .ToDictionary?


c# c# 
Updated on 18-Dec-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By