Reputation: 2037
I have the following dictionary:
Dictionary<int, string> selectedDrivers = new Dictionary<int, string>()
{
{ 1, "Michael Schumacher" },
{ 2, "Michael Schumacher" },
{ 3, "Jensen Button" }
};
What I try to achieve is a linq query that will find the duplicates, retrieve the name only once, along with the positions this duplicate has. So, for example, the above would result in "Michael Schumacher" 1 2
. I would like the result to go into a Dictionary<string, List<int>>
.
Getting excited fast, this little nugget I wrote seems to be going in the right direction, except that I have Michael Schumacher twice. And it's not yet in a Dictionary<string, List<int>>
var a = selectedDrivers.GroupBy(a => a.Value).
Where(b => b.Count() > 1);
Upvotes: 0
Views: 1127
Reputation: 13496
var a = selectedDrivers.GroupBy(a=>a.Value)
.Where(g=>g.Count() > 1)
.ToDictionary(g => g.Key, g => g.Select(a=>a.Key).ToList());
Upvotes: 3