2017-03-07 56 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() 
     });