c# - 将 Linq 查询表达式转换为点符号

标签 c# linq

我刚刚开始学习 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/

相关文章:

c# - 编辑合并排序以按 C# 降序排序

c# - Ninject.MVC 构造函数注入(inject),其中注入(inject)对象的构造函数采用参数

javascript - js 无法在 ASP.NET 中运行

c# - 如何为多对多关系编写简单的 LINQ 报告?

c# - 在 DataTable 上使用 LINQ 的 .Any()

.net - WPF TreeView 绑定(bind)

c# - 模仿 C# 的 List<List<int>> 的 C 数据结构?

c# - 查找 TimeSpans 集合的平均值

c# - 如何按 2 列对 DataTable 进行排序,使用 NULL,也许使用 LINQ?

c# - 你如何动态调用linq字段