4
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);
 }

结果是:5 6 7 5 2 5

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

4

3 回答 3

5

使用GroupByLINQ 方法:

ListIdProducts
    .GroupBy(i => i)
    .Select(g => new { Value = g.Key, Count = g.Count() });
于 2012-04-17T14:12:18.200 回答
3
var query1 = from a in ListIdProducts 
                         group a by new { a } into g
                         select new
                         {
                             item = g.Key,
                             itemcount = g.Count()
                         };
于 2012-04-17T14:18:41.993 回答
2

这是一个相当标准的分组问题。

//untested
var IdProducts = from a in me.ProductKeywords 
                 where a.Keyword == item.Id 
                 group by a.Product.Value into g
                 select g.Count();
于 2012-04-17T14:13:42.340 回答