我目前有这个linq声明:
from s in SubContentRevisions
where s.SubContentID.Equals("e3f319f1-65cc-4799-b84d-309941dbc1da")
&& s.RevisionNumber == (SubContentRevisions.Max(s1 => s1.RevisionNumber))
select s
生成此SQL(根据LINQPad):
-- Region Parameters
DECLARE @p0 UniqueIdentifier = 'e3f319f1-65cc-4799-b84d-309941dbc1da'
-- EndRegion
SELECT [t0].[SubContentRevisionID], [t0].[SubContentID], [t0].[RevisionNumber], [t0].[RevisionText], [t0].[CreatedDate], [t0].[ModifiedDate]
FROM [SubContentRevision] AS [t0]
WHERE ([t0].[SubContentID] = @p0) AND ([t0].[RevisionNumber] = ((
SELECT MAX([t1].[RevisionNumber])
FROM [SubContentRevision] AS [t1]
)))
如何让它生成此SQL语句?我似乎找不到任何相关的东西。 (我需要它将where子句添加到子查询中)
-- Region Parameters
DECLARE @p0 UniqueIdentifier = 'e3f319f1-65cc-4799-b84d-309941dbc1da'
-- EndRegion
SELECT [t0].[SubContentRevisionID], [t0].[SubContentID], [t0].[RevisionNumber], [t0].[RevisionText], [t0].[CreatedDate], [t0].[ModifiedDate]
FROM [SubContentRevision] AS [t0]
WHERE ([t0].[SubContentID] = @p0) AND ([t0].[RevisionNumber] = ((
SELECT MAX([t1].[RevisionNumber])
FROM [SubContentRevision] AS [t1]
WHERE [SubContentID] = @p0 -- **********Adds the where clause**********
)))
答案 0 :(得分:4)
我想你想要:
from s in SubContentRevisions
where s.SubContentID.Equals("e3f319f1-65cc-4799-b84d-309941dbc1da")
&& s.RevisionNumber == (SubContentRevisions.Where(s.SubContentID.Equals("..."))
.Max(s1 => s1.RevisionNumber))
select s
或者,更清楚:
var specificSubContents = SubContentRevisions.Where(s =>
s.SubContentID.Equals("e3f319f1-65cc-4799-b84d-309941dbc1da")
var query = from s in specificSubContents
where s.RevisionNumber = s.Max(s1 => s1.RevisionNumber)
select s;
或者,听起来你实际上可以做到:
var latest = (from s in SubContentRevisions
where s.SubContentID.Equals("e3f319f1-65cc-4799-b84d-309941dbc1da")
orderby s.RevisionNumber descending
select s).FirstOrDefault();
答案 1 :(得分:0)
如何将where子句添加到子查询(max):
from s in SubContentRevisions
where s.SubContentID.Equals("e3f319f1-65cc-4799-b84d-309941dbc1da")
&& s.RevisionNumber == (SubContentRevisions
.Where(s1 => s1.SubContentID.Equals(s.SubContentID))
.Max(s1 => s1.RevisionNumber))
select s