我的数据库中有以下视图
SELECT YEAR(Received) AS YEAR,
MONTH(Received) AS MONTH,
LEFT(DATENAME(MONTH, Received), 3) AS MMM,
COUNT(Received) AS Submissions,
COUNT(Quoted) AS Quotes,
COUNT(Bound) AS Binders,
COALESCE (SUM(BndPremium), 0) AS Premium,
ProducerID
FROM dbo.Quote AS Q WITH (NOLOCK)
WHERE (Received >= DATEADD(year, - 1, GETDATE()))
GROUP BY ProducerID, YEAR(Received), MONTH(Received), DATENAME(MONTH, Received)
我已将视图添加到我的EDMX中。我这样查询视图:
var submissions = from s in db.WSS_PortalSubmissions
where s.ProducerID == ID
select s;
提交的结果'但是,第一个月是12份,而不是过去12个月的结果。今天在Linq运行查询我从2016年4月获得了12份结果。如果我在SSMS中运行查询,我会得到预期结果,即过去12个月的列表。
我试过.ToList(),. ToArray(),甚至尝试过对结果进行一些排序,但它并没有改变。它只给了我第一个月的12份。我无法看到的任何理由?
答案 0 :(得分:2)
我会更改视图,以便在没有列的情况下存在唯一列,并确保将其映射为EF中的主键。
如果这不是一个选项,请尝试将代码更改为
var submissions = from s in db.WSS_PortalSubmissions.AsNoTracking()
where s.ProducerID == ID
select s;
这样EF就不会跟踪实体,这应该强制它只返回查询结果而不尝试基于主键进行跟踪。