I have a list that I want to put in the dictionary, for simplicity, the inserted values will be the same.
I can use the foreach loop.
List<string> list = new List<string>(); list.Add("Earth"); list.Add("Wind"); list.Add("Fire"); list.Add("Water"); list.Add("Water");
The above work.
But I want to use ToDictionary to do the same as follows -
Dictionary<string, int> myDictionary2 = list.ToDictionary(i => i, i => 1);
Of course, this fails because I add Water twice.
What is the correct way to check for duplicate entries when using ToDictionary?
Bryan
source share