Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace dictionary key with a property of itself

Let’s say I have a Dictionary defined this way:

var Dict = Dictionary<Person, List<Car>>

With both Person and Car being objects. If I wanted to turn that dictionary into this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var Dict = Dictionary<Person.Id, List<Car>>

What process would it need to go through?

>Solution :

You can try to use ToDictionary method that creates another Dictionary by keys which you expect.

var result = Dict.ToDictionary(x=>x.Key.Id,z=>z.Value)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading