C# – Distinct Values in Dictionary

cdistinctlinqnet

I'm trying to loop over distinct values over a dictionary list:

So I have a dictionary of key value pairs .

How do I get just the distinct values of string keys from the dictionary list?

Best Answer

var distinctList = mydict.Values.Distinct().ToList();

Alternatively, you don't need to call ToList():

foreach(var value in mydict.Values.Distinct())
{
  // deal with it. 
}

Edit: I misread your question and thought you wanted distinct values from the dictionary. The above code provides that.

Keys are automatically distinct. So just use

foreach(var key in mydict.Keys)
{
  // deal with it
}