2015-01-12 29 views
-1

我可能錯過了一些顯而易見的東西 - 但是當我在字典或集合上執行.Count時,我得到的錯誤計數與字典或集合中元素的實際數量相比。Collection/Dictionary.Count將不同的值返回到實際的元素數量?

我有計算字符串中字數的代碼,並返回Collection<KeyValuePair<string, uint>>

這裏是我的代碼:

string myString = String.Format("Hello world this is test test test test hi"); 

var result = WordCounter.GetWordCollection(myString); 

result.Dump(); //Using LINQPad .Dump() method 

Console.WriteLine ("Counted {0} words in {1}ms", result.Count, stopwatch.Elapsed.Milliseconds); 

foreach (var word in result) 
{ 
    Console.WriteLine ("{0} - {1}", word.Key, word.Value); 
} 

結果:

enter image description here

什麼錯?

編輯:我正在計算沒有意識到的唯一字的數量,就像評論/回答說的那樣。我需要使用result.Sum(n => n.Value)

+0

什麼'GetWordCollection'返回?創建自己的集合類型當然很容易,它可以返回任何你想要的。 – Servy

+0

你期望得到什麼? – SLaks

+0

它返回集合> – user9993

回答

1

它正常工作 - 字典中有七個鍵,每個鍵的值都代表它們在源文本中的次數。七計數意味着有七個獨特的詞。總結這些值將會給你包括重複的總字數。

相關問題