Why is LINQ for Entities creating a subquery for me?

I am using .NET 4 and Entity Framework to create 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 request with 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 NVARCHAR (255) .

This seems strange to me because I don’t see the need for a subquery. Why does EF do this for me, and is there something I can do to not accept what I assume is a performance hit from such a request?

+3
source share
1 answer

, , . , , EF "" , . , , EF . , , .

, EF , , , LINQ SQL-. , , , , , , . , , "" , , SQL Server.

+3

Source: https://habr.com/ru/post/1759431/


All Articles