I'm using .NET 4 and the Entity Framework to construct a simple query. Here's the C# code:
return Context.Files.Where(f => f.FileHash != 40)
.OrderByDescending(f => f.Created)
.Take(5);
When I trace the query using ObjectQuery.ToTraceString()
, I find the following subquery:
SELECT TOP (5)
[Project1].[ID] AS [ID],
-- <snip> lots of columns
[Project1].[FileHash] AS [FileHash]
FROM ( SELECT
[Extent1].[ID] AS [ID],
-- <snip> lots of columns
[Extent1].[FileHash] AS [FileHash]
FROM [dbo].[Files] AS [Extent1]
WHERE (LEN([Extent1].[FileHash])) <> 40
) AS [Project1]
ORDER BY [Project1].[Created] DESC
FileHash is defined as an NVARCHAR(255).
This seems strange to me, as I don't see any need for a subquery. Why is EF doing this for me, and is there something I can do to not take what I assume is a performance hit from such a query?