请解释此sql查询的作用

时间:2019-05-28 15:30:58

标签: sql sql-server entity-framework linq tsql

我有这个sql查询是由linqpad生成的,它工作正常,但是我没有得到如何?

  

Linq语句

Documents.GroupBy(p => new { p.PatientId, p.DocumentType })
                    .Select(g => g.OrderByDescending(p => 
p.CreationDateTime).FirstOrDefault().DocumentId)
  

sql查询

SELECT (
    SELECT [t3].[DocumentId]
    FROM (
        SELECT TOP (1) [t2].[DocumentId]
        FROM [Document] AS [t2]
        WHERE ([t1].[PatientId] = [t2].[PatientId]) AND ([t1].[DocumentType] = [t2].[DocumentType])
        ORDER BY [t2].[CreationDateTime] DESC
        ) AS [t3]
    ) AS [value]
FROM (
    SELECT [t0].[PatientId], [t0].[DocumentType]
    FROM [Document] AS [t0]
    GROUP BY [t0].[PatientId], [t0].[DocumentType]
    ) AS [t1]

0 个答案:

没有答案