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
最佳答案
使用GroupBy
LINQ 方法:
ListIdProducts
.GroupBy(i => i)
.Select(g => new { Value = g.Key, Count = g.Count() });
关于c# - 如何从 List<int> 中获取重复次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10192878/