41

I was following the answer to another question, and I got:

// itemCounter is a Dictionary<string, int>, and I only want to keep
// key/value pairs with the top maxAllowed values
if (itemCounter.Count > maxAllowed) {
    IEnumerable<KeyValuePair<string, int>> sortedDict =
        from entry in itemCounter orderby entry.Value descending select entry;
    sortedDict = sortedDict.Take(maxAllowed);
    itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */);
}

Visual Studio's asking for a parameter Func<string, int> keySelector. I tried following a few semi-relevant examples I've found online and put in k => k.Key, but that gives a compiler error:

'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,int>>' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' has some invalid arguments

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Kache
  • 15,647
  • 12
  • 51
  • 79

3 Answers3

65

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);
Rotsor
  • 13,655
  • 6
  • 43
  • 57
  • Thanks a lot for your elaboration! So in C#, `pair => pair.Key` is of type `Func`? How do you declare one of those? (So that one could do `sortedDict.ToDictionary(funcKey, funcVal);`?) – Kache Jun 17 '10 at 23:31
  • 3
    Actually, I'd suggest you not use C# LINQ syntax because it kind of hides from you what methods you really call, and looks alien for C# language. I never use it because I think it's ugly. Your sample could be written in C# without linq like this: `sortedDict = itemCounter.OrderByDescending(entry => entry.Value)`. Not longer, right? – Rotsor Jun 17 '10 at 23:43
  • 2
    I don't see a `OrderByDescending` method for `Dictionary`. – Kache Jun 21 '10 at 14:26
9

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);
CB01
  • 371
  • 3
  • 7
-1

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);
  • This won't return the expected results, as you are doing the `Take` before you have done the sort – mikeyq6 Mar 19 '19 at 11:12