将Linq查询表达式转换为点表示法

kr1*_*r13 4 c# linq

我刚开始学习LINQ.我编写了以下linq表达式来获取列表中重复3次的数字.

 var query = from i in tempList
             where tempList.Count(num => num == i) == 3
             select i;
Run Code Online (Sandbox Code Playgroud)

我想知道如何将其转换为点符号.

Tim*_*ter 5

你可以使用Enumerable.GroupBy:

var query = tempList
            .GroupBy(i => i)
            .Where(g => g.Count() == 3)
            .Select(g => g.Key); 
Run Code Online (Sandbox Code Playgroud)

例如:

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));
Run Code Online (Sandbox Code Playgroud)

结果:3,7