Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想知道列表中每个项目的计数_C#_List_Count - Fatal编程技术网

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

C# 我想知道列表中每个项目的计数,c#,list,count,C#,List,Count,所以,我有一个投票名单 List<string> Votes = new List<string>(); 最多有三个不同的字符串。我想数一数。这是c 我已经看过了前面的问题,但没有发现任何类似的问题,而问题比我的复杂得多。 对不起,如果这个问题已经被回答了嗯,林克,像那样吗 List<String> votes = new List<String>() { "Yes", "No", "Yes", "No", "?" }; ..

所以,我有一个投票名单

List<string> Votes = new List<string>();
最多有三个不同的字符串。我想数一数。这是c

我已经看过了前面的问题,但没有发现任何类似的问题,而问题比我的复杂得多。 对不起,如果这个问题已经被回答了

嗯,林克,像那样吗

  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); 
您可以使用GroupBy:

现在,如果您想知道每个字符串的计数,请使用Enumerable.count:

另一种方法是使用ToLookup:

查找有它的优点,它使您能够像字典一样查找特定的元素。因此,您可以通过以下方式获得百事可乐的数量:

int pepsiCount = voteLookup["Pepsi"].Count();
如果listcount中没有这样的字符串,这不会导致异常,它将为0

如果您想使其不区分大小写,那么请平等对待百事可乐和百事可乐:

var voteLookup = Votes.ToLookup(s => s, StringComparer.InvariantCultureIgnoreCase);
如果您只有一个字符串列表,那么就可以使用Count属性

List<string> Votes = new List<string>();
// populate list here
Console.WriteLine(Votes.Count);
试试这个

var l = new List();
l.Add("abcd");
l.Add("abcd123");
l.Add("abcd1234");

var d = l.ToDictionary<string,int>(k=>k, v=>v.Length);

var count = d["abcd"]; //result would be 4.

如果您只是尝试获取列表中唯一项目的总数:

int count = (from x in lst select x).Distinct().Count();

不是最好的代码,但它可以工作:

int count1 = 0;
int count2 = 0;
foreach (String answer in Votes)
{
  if (answer == "yes")
  {
    count1++;
  }
  if (answer == "no")
  {
    count2++;
  }
}

你能给我们一些你的代码来帮助我们理解你的问题并帮助你吗;我正试图从中获得所有不同的选票。因此,有5人投票支持百事可乐,3人投票支持可口可乐,5人投票反对indifferent@JulesVerdonck如果你是指新的名单;你应该在你的问题中表现出来。细节很重要。我强烈建议你阅读本书中的内容和其他主题。你的问题现在的措辞我们无法真正确定问题所在。欢迎来到SO!别让这件事吓跑你-你应该更新你的问题,准确地说明这一点,而不是张贴图片。虽然您已经使用GroupBy获得了一些有效答案。Count是IEnumerable的扩展,因此它也可以工作。无论如何,OP想要列表中不同项目的计数。@ArthurRey可能是,但我想OP还没有明确说明。@ArthurRey从目前的问题编写方式来看,这听起来像是一个最多有三个值的列表,他想知道在某个时间列表中有多少个值。@Nik如果你看看刚才发布的OP的图像链接,我想这会更清楚地表明Arthur是对的。这会告诉你每个字符串有多少个字符。如果列表中有重复的字符串,就会抛出一个异常,这里就是这种情况。首先,这不是OP想要的,请查看投票结果。更进一步,你为什么不做lst.Distinct.Count呢?
var l = new List();
l.Add("abcd");
l.Add("abcd123");
l.Add("abcd1234");

var d = l.ToDictionary<string,int>(k=>k, v=>v.Length);

var count = d["abcd"]; //result would be 4.
int count = (from x in lst select x).Distinct().Count();
int count1 = 0;
int count2 = 0;
foreach (String answer in Votes)
{
  if (answer == "yes")
  {
    count1++;
  }
  if (answer == "no")
  {
    count2++;
  }
}