Linq to Entities - strange results

I have the following view in my database

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)

      

And I added a view to my EDMX. I am requesting a view this way:

var submissions = from s in db.WSS_PortalSubmissions
                  where s.ProducerID == ID
                  select s;

      

The results in the "views", however, are 12 copies of the first month, not the last 12 months. By running a query in Linq today, I am getting 12 copies of the results from April 2016. If I run a query in SSMS, I get the expected results, listing the last 12 months.

I tried .ToList (), ToArray () even tried to do some sort of sorting on the results, but that didn't change. This only gives me 12 copies of the first month. Any reasons why I can't see?

+3


source to share


1 answer


I would change the view so that there is a unique column (s) if not already, and make sure it shows up as a primary key in EF.

If that's not an option, try changing your code to



var submissions = from s in db.WSS_PortalSubmissions.AsNoTracking()
       where s.ProducerID == ID
       select s;

      

so that EF does not track the object, it forces it to just accurately return the query results without trying to track based on the primary key.

+1


source







All Articles