I fixed the formatting of the code in your question.
However, your attempt to get a sorted dictionary will not work.
Using the
.ToDictionary(...)
turns it back into a
regular Dictionary
which does not preserve any ordering.
It looks like you can just use the query to make an
IEnumerable<KeyValuePair<string, int>>
and iterate over that:
var sortedWordCounts = from entry in dictionary orderby entry.Value descending select entry;
int count = 1;
Console.WriteLine("---- Most Frequent Terms in the File: " + filename + " ----");
Console.WriteLine();
foreach (var pair in sortedWordCounts)
{
Console.WriteLine(count + "\t" + pair.Key + "\t" + pair.Value);
count++;
}
Console.ReadKey();
If you really need to keep the collection in the sorted order, you should use
.ToList()
or
.ToArray()
.