我正试图从我的数据库中获得最受欢迎的3种产品。我有一个名为clicks的表,它包含单击的产品以及其他一些不重要的字段(如IP、date等)。
我希望查询数据库以计数产品名称相同的行总数,并返回总数和产品名称。
我的linq查询没有做我希望的事情。我的代码:
var results == (from r in this._dc.Clicks
group r by r
into g
orderby g.Count() descending
select new { Total = g.Count(), Productc= g.Key.Product }).Take(3).ToList();问题是,这将返回结果,如
11, Shoes
5, Shirts
4, Shirts 我本以为结果会是
11, Shoes
9, Shirts
something else为什么我的Linq不显示总数?
发布于 2014-10-22 06:44:40
你的分组并不是真正的分组。您需要找到一个分组密钥,而不是实体本身。显然你应该按Product分组。
var query =
(from click in this._dc.Clicks
group click by click.Product into g
let count = g.Count()
orderby count descending
select new { Product = g.Key, Total = count }).Take(3);https://stackoverflow.com/questions/26501555
复制相似问题