Home > Software design >  Replace dictionary key with a property of itself
Replace dictionary key with a property of itself

Time:03-16

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:

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

What process would it need to go through?

CodePudding user response:

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)
  • Related