I was following the answer to another question, and I got:
// itemCounter is a Dictionary, and I only want to keep
// key/value pairs with
You are specifying incorrect generic arguments. You are saying that TSource is string, when in reality it is a KeyValuePair.
This one is correct:
sortedDict.ToDictionary<KeyValuePair<string, int>, string, int>(pair => pair.Key, pair => pair.Value);
with short version being:
sortedDict.ToDictionary(pair => pair.Key, pair => pair.Value);
The question is too old but still would like to give answer for reference:
itemCounter = itemCounter.Take(maxAllowed).OrderByDescending(i => i.Value).ToDictionary(i => i.Key, i => i.Value);
I believe the cleanest way of doing both together: sorting the dictionary and converting it back to a dictionary would be:
itemCounter = itemCounter.OrderBy(i => i.Value).ToDictionary(i => i.Key, i => i.Value);