praveen
praveen

Reputation: 12271

Populating data to a nested Dictionary Collection

I have a collection List<Employee> which contain Properties like

Name ,EmployeeID,Address,JobTitle and so on

For every employee there are few column which changes and based upon these few columns i need to update my Active Directory.

So i thot of creating a isEditable field for each column which will signify whether the column has changed or not for a particular employee . But it turns out that i need to create this field for all the columns and the number of columns also changes frequently .

So I came up with nested dictionary collection

 Dictionary<int, Dictionary<object, bool>>

which will store the Employee ID as the key and the object type will store all the column names and bool by default will be false

But i don't know how to populate the above nested collection with List<Employee> Collection .

This is what i have tried till now

Dictionary<int, Dictionary<object, bool>> _isEditable = new Dictionary <int, Dictionary<object, bool>>();

foreach (var item in _empColl )
{
   foreach (var type in _empColl .GetType ().GetProperties ())
   {
       _isEditable.Add  (item.EmployeeID ,dict.Add (type.Name,false));
   }
}

But its throwing error .I'm trying to get the metadata (column names ) from the _empColl for each EmployeeID

Upvotes: 1

Views: 281

Answers (1)

cuongle
cuongle

Reputation: 75296

You can use LINQ below to get the result:

 var _isEditable = _empColl.ToDictionary(item => item.EmployeeID,
                                         item => item.GetType()
                                                  .GetProperties()
                                                  .ToDictionary(pro => pro.Name, 
                                                                pro => false));

Upvotes: 2

Related Questions