views:

130

answers:

3

hey,

I've a dictionary, which i sorted by value with linq, how can i get those sorted value from the sorted result i get

that's what i did so far

Dictionary<char, int> lettersAcurr = new Dictionary<char, int>();//sort by int value
var sortedDict = (from entry in lettersAcurr orderby entry.Value descending select entry);

during the debug i can see that sortedDic has a KeyValuePar, but i cant accesses to it

thanks for help

+1  A: 

sortedDict is IEnumerable<KeyValuePair<char, int>> iterate it

Andrey
A: 

Just iterate over it.

foreach (var kv in sortedDict)
{
     var value = kv.Value;
     ...
}
tvanfosson
A: 

If you just want the char values you could modify your query as:

var sortedDict = (from entry in lettersAcurr orderby entry.Value descending select entry.Key);

which will give you a result of IEnumerable<char>

If you want it in a dictionary again you might be tempted to

var q = (from entry in lettersAcurr orderby entry.Value descending select entry.Key).ToDictionary(x => x);

but do bare in mind that the dictionary will not be sorted, since the Dictionary(Of T) will not maintain the sorted order.

Cornelius