0

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);
1
  • You can't do that in only one Linq request I think Commented Jun 29, 2011 at 19:44

1 Answer 1

3
var a = selectedDrivers.GroupBy(a=>a.Value)
            .Where(g=>g.Count() > 1)
            .ToDictionary(g => g.Key, g => g.Select(a=>a.Key).ToList());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.