Why is a linq-to-sql query translated into a subquery?

Why is this linq request:

(from c in Orders select new { Id=c.Id, DeliveryDate = c.DeliveryDate.Value }).Take(10) 

translates to

 SELECT TOP (10) [t1].[Id], [t1].[value] AS [DeliveryDate] FROM ( SELECT [t0].[Id], [t0].[DeliveryDate] AS [value] FROM [Orders] AS [t0] ) AS [t1] 

but when I change DeliveryDate = c.DeliveryDate.Value to DeliveryDate = c.DeliveryDate , the SQL query looks as simple as:

 SELECT TOP (10) [t0].[Id], [t0].[DeliveryDate] FROM [Orders] AS [t0] 
+4
source share
2 answers

I think this is because the LINQ2SQL translator LINQ2SQL underestimated. Using the "property" ( Value ) starts the creation of a subquery, which is unnecessary.

It is worth noting that any DBMS worthy of its salt will generate identical query plans for both SQL queries, so in the end it does not matter in any case.

+2
source

There may be an error / error issue that is not optimized. I can not explain it otherwise.

0
source

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


All Articles