c# – 为什么linq-to-sql查询被翻译成子查询?

为什么这个
linq查询:

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

被翻译成

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

但当我将DeliveryDate = c.DeliveryDate.Value更改为DeliveryDate = c.DeliveryDate时,SQL查询看起来很简单:

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

最佳答案 我认为这是因为LINQ2SQL的翻译器资源不足.使用“属性”(Value)会触发子查询的创建,这种情况证明是不必要的.

值得注意的是,任何值得盐的RDBMS都会为两个SQL查询生成相同的查询计划,因此最终无论如何都无关紧要.

点赞