linq到具有聚合函数的SQL

| 我花了大量时间尝试将下面的查询转换为linq到实体。
SELECT ItemCode, AVG([Count]) As [Count]
FROM [tblHistory]
GROUP BY ItemCode
ORDER BY [Count] desc
在我的实体框架edmx中,我有一个带有字段的
tblHistory
{ ItemCode(int), Count(int), Date(datetime) }
我可以按一列分组并显示该特定列(在这种情况下为“ 3”),但是在相同的上下文中访问“ 4”会给我带来麻烦。 我尝试使用的方法之一如下:
 var query = from p in Context.tblHistories
             group p by p.ItemCode into g
             select new tblHistory
                        {
                            ItemCode = g.Key,
                            Count = from c in g select new { c.Count }
                        };
请让我知道,如果有其他了解此信息的人需要提供其他信息,我们将乐意提供。     
已邀请:
        
tblHistories.GroupBy(p => p.ItemCode)
         .Select(g => new { ItemCode = g.Key, Count = g.Average (c => c.Count)})
         .OrderByDescending(x => x.Count)
         .AsEnumerable()
         .Select(x => new tblHistory{ ItemCode = x.ItemCode, Count = (int)x.Count })
    

要回复问题请先登录注册