[英]How can I use Linq for this?
Imagine I have the following arrays: 想象一下,我有以下数组:
int[] player = new int[5] { 1, 2, 3, 4, 4 };
int[] computer = new int[5] { 1, 1, 1, 3, 5 };
How can I use a Linq query to fetch the amount of times each number is in each collection? 我如何使用Linq查询来获取每个数字在每个集合中的次数?
This would give you the frequency of each number in the player
array, same would work for the computer
array: 这将为您提供player
阵列中每个数字的频率,同样适用于computer
阵列:
var playerFrequencies = player.GroupBy(n => n)
.Select(g => new { Number = g.Key,
Frequency = g.Count() })
.ToList();
var count =
from i in player
group i by i into g
select new { Number = g.Key, Count = g.Count() }
int HowManyTimes(IEnumerable<int> numberCollection, int numberToFind){
return numberCollection.Count(x = >x == numberToFind);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.