我刚刚开始学习 LINQ。我编写了以下 linq 表达式来获取列表中重复 3 次的数字。
var query = from i in tempList
where tempList.Count(num => num == i) == 3
select i;
我想知道如何将其转换为点符号。
最佳答案
您可以使用 Enumerable.GroupBy
:
var query = tempList
.GroupBy(i => i)
.Where(g => g.Count() == 3)
.Select(g => g.Key);
例如:
var tempList = new List<Int32>(){
1,2,3,2,2,2,3,3,4,5,6,7,7,7,8,9
};
IEnumerable<int> result = tempList
.GroupBy(i => i)
.Where(g => g.Count() == 3)
.Select(g => g.Key);
Console.WriteLine(string.Join(",",result));
结果:3,7
关于c# - 将 Linq 查询表达式转换为点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487905/