我想知道列表中每个项目的计数

-9 c# list count

所以,我有一张投票清单.

List<string> Votes = new List<string>();
Run Code Online (Sandbox Code Playgroud)

最多有三个不同的字符串.我想得到他们的数量.这是在c#中.

我已经查看了之前的问题,但没有找到类似的东西,没有问题比我的更复杂.对不起,如果已经回答了这个问题

Dmi*_*nko 5

呃,Linq那样?:

  List<String> votes = new List<String>() {
    "Yes", "No", "Yes", "No", "?"
  };
  ...
  String report = String.Join(Environment.NewLine, votes
    .GroupBy(item => item)
    .Select(chunk => String.Format("{0} votes, count {1}", chunk.Key, chunk.Count())) 
  );

  Console.Write(report); 
Run Code Online (Sandbox Code Playgroud)