我的数据库中有vw_AllNomenLost
视图
SELECT TOP (100) PERCENT ProducerName, NomenCode, SUM(ErrorCount) AS ErrorCount
FROM dbo.NomenLost
GROUP BY ProducerName, NomenCode
ORDER BY ErrorCount DESC
我将它添加到实体模型。
我想通过Producer排名前10行并按ErrorCount
db.vw_AllNomenLost.Where(a => a.ProducerName == producer).OrderByDescending(a => a.ErrorCount).Take(10);
在调试器中我得到:
SELECT TOP (10)
[Project1].[NomenCode] AS [NomenCode],
[Project1].[ProducerName] AS [ProducerName],
[Project1].[ErrorCount] AS [ErrorCount]
FROM (
SELECT
[Extent1].[ProducerName] AS [ProducerName],
[Extent1].[NomenCode] AS [NomenCode],
[Extent1].[ErrorCount] AS [ErrorCount]
FROM (
SELECT
[vw_AllNomenLost].[ProducerName] AS [ProducerName],
[vw_AllNomenLost].[NomenCode] AS [NomenCode],
[vw_AllNomenLost].[ErrorCount] AS [ErrorCount]
FROM [dbo].[vw_AllNomenLost] AS [vw_AllNomenLost]) AS [Extent1]
WHERE ([Extent1].[ProducerName] = @p__linq__0) AND (@p__linq__0 IS NOT NULL)
) AS [Project1]
ORDER BY [Project1].[ErrorCount] DESC
如果我执行
SELECT TOP (10) *
FROM vw_AllNomenLost
where ProducerName = @producer
Order by ErrorCount desc
在Management Studio中,我得到了正确的结果。但我的linq查询返回一些重复的行。
NomenCode ErrorCount
31190RRAA00 11
31190RRAA00 11
31190RRAA00 11
31190RRAA00 11
怎么可能?我不明白。我做错了什么?非常感谢您的阅读和抱歉太久的问题。