How do you get the duplicate key that ToDictionary() has failed on?
问题 I'm creating a Dictionary object, using IEnumerable 's ToDictionary() extension method: var dictionary = new Dictionary<string, MyType> (myCollection.ToDictionary<MyType, string>(k => k.Key)); When it executes, it throws the following ArgumentException : An item with the same key has already been added. How do I get it to tell me what the duplicate key is? 回答1: Get the duplicate keys: var duplicateKeys = myCollection .GroupBy(k => k.Key) .Where(g => g.Count() > 1) .Select(g => g.Key); 回答2: If