I have a strange issue. The code below, once executed, does not use any form of ORDER BY in the sql statement, and so it does not return the intented records.
var last5CommesseRisorsaPjId = db.Attivita .Where(a => a.IdRisorsa == loggedUser.Id) .OrderByDescending(a => a.Data) //this looks ignored .Select(a => a.FasiCommessa.Commesse.AxProjId) .Distinct() .Take(5) .ToList(); SQL: (using db.Database.Log):
SELECT [Limit1].[AxProjId] AS [AxProjId] FROM ( SELECT DISTINCT TOP (5) [Extent3].[AxProjId] AS [AxProjId] FROM [dbo].[Attivita] AS [Extent1] LEFT OUTER JOIN [dbo].[FasiCommessa] AS [Extent2] ON [Extent1].[IdFaseCommessa] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[Commesse] AS [Extent3] ON [Extent2].[IdCommessa] = [Extent3].[Id] WHERE [Extent1].[IdRisorsa] = 'ecaffee0-5aeb-45cf-8d40-218ff63a2108' ) AS [Limit1] I would expect an ORDER BY [Extent1].[Data] DESC after the WHERE clause.
What's wrong?