2017-03-07 55 views
-6

我有這樣的代碼爲C#C#:循環或數組

var wordCounts1 = myInterface.GetWordCounts(new[] { 
"one", "one", "two", "three", "three",}); 

預期的輸出應該是:

{"one", 2}, {"two", 1}, {"three",2} 

我應該怎麼用得到這個輸出迴路或數組?

+0

你已經有一個數組。使用循環! – Sinatr

+0

嘗試'GroupBy'和'Count' – slawekwin

+0

如果您需要在數組中使用LINQ –

回答

0

分組由字應努力

new[] { "one", "one", "two", "three", "three" } 
    .GroupBy(x => x) 
    .Select(x => new 
     { 
      Word = x.Key, 
      Count = x.Count() 
     });