如何从List <int>获取重复次数

ale*_*vdi 4 c# linq linq-to-sql

List<int> ListIdProducts = new List<int>();
var IdProductKey = from a in me.ProductKeywords where a.Keyword == item.Id select a;
 foreach (var item2 in IdProductKey)
 {
   ListIdProducts.Add(item2.Product.Value);
 }
Run Code Online (Sandbox Code Playgroud)

结果是:5 6 7 5 2 5

我需要得到以下5 = 3,6 = 1,7 = 1,2 = 1

mel*_*okb 5

使用GroupByLINQ方法:

ListIdProducts
    .GroupBy(i => i)
    .Select(g => new { Value = g.Key, Count = g.Count() });
Run Code Online (Sandbox Code Playgroud)