使用linq 进行Group by 查询

使用linq 进行Group by 查询
上图是数据库表

需求是要统计出来 不合格 top 10

其中 NGcode =200代表合格 其他均不合格

SQL 语句 如下
SELECT top 10 [NGCode] AS [DefectCode], COUNT(1) AS [Count]
FROM [dbo].[tblPassStationData] where NGCode!=200
GROUP BY [NGCode] order by [Count] desc

结果使用linq 进行Group by 查询

如果 用 linq 来实现 可以 用一下代码:

var res = (from item in _Context.PassStations where item.NGCode!=200
group item by item.NGCode into row
select new DefectInfo
{
DefectCode = row.Key,
//Count = row.Count( p=>p.NGCode!=200)
Count = row.Count()
}).Take(10).OrderByDescending( de =>de.Count).ToList();
return res;

我 不太会编程,凑合用吧 ,希望老哥能指导