当您不知道其背后的数据时,不知道仅通过查看示例 SQL 查询来回答这个问题有多容易,但无论如何我都会尝试。这是一个 SQL 查询,用于列出源代码控制数据库中的文件——并排显示源文件路径及其最近签入的版本路径:
WITH [recentCheckins] AS (
SELECT [SourceFileID], MAX([Version]) AS [LatestVersion]
FROM [Checkin]
GROUP BY [SourceFileID]
) SELECT
[sf].[Path], -- current version
[ci].[ArchivePath] -- most recent previous checkin
FROM
[recentCheckins]
INNER JOIN [SourceFile] [sf] ON [recentCheckins].[SourceFileID]=[sf].[ID]
INNER JOIN [Checkin] [ci] ON
[recentCheckins].[SourceFileID]=[ci].[SourceFileID] AND
[recentCheckins].[LatestVersion]=[ci].[Version]
这是我能做的最多的事情:
var query2 =
from latestCheckins in (
from checkin in _project.Checkin
group checkin by checkin.SourceFileID into results
select new
{
SourceFileID = results.Key,
LatestCheckinVersion = results.Max(s => s.Version)
})
join sourceFile in _project.SourceFile on latestCheckins.SourceFileID equals sourceFile.ID
join checkins in _project.Checkin on // fails on this line, does not like join keyword here
new { latestCheckins.SourceFileID, latestCheckins.LatestCheckinVersion }
equals new { checkins.SourceFileID, checkins.Version }
select new
{
Filename = sourceFile.Path,
CheckinDate = checkins.DateCreated,
TaskID = checkins.TaskID,
Comments = checkins.Comments,
Version = latestCheckins.LatestCheckinVersion.ToString(),
NewFile = _project.GetSourceFilename(sourceFile.Path),
OldFile = checkins.ArchivePath,
ID = sourceFile.ID
};