Home > Net >  ToDictionary how can i find the duplicate key
ToDictionary how can i find the duplicate key

Time:07-09

I want to correct the database but I can't find the duplicate object. Is there a way for me to find the key that is having problems?

"An item with the same key has already been added."}

    try
    {
        var dict = new SortedDictionary<long, long>(ReturnGetReviewTemplates.ToDictionary(tuple => tuple.Item1.Id, tuple => tuple.Item2.ReviewId));
    }
    catch(Exception e)
    {
        string error = e.InnerException.Message;
    }

CodePudding user response:

use GroupBy and inspect groups larger than 1:

var dupes = ReturnGetReviewTemplates
               .GroupBy(t => t.Item1.Id)
               .Where(g => g.Count() > 1)
               .Select(g => g.Key)
               .ToArray();

set breakpoint or write a string to Output

Debug.WriteLine(string.Join(", ", dupes));
  •  Tags:  
  • c#
  • Related