这将稍微取决于您的映射,但这样的事情应该可以工作:
Table1 aliasTable1 = null, secondAliasTable1 = null, thirdAliasTable1 = null;
Table2 aliasTable2 = null;
var result = session.QueryOver<Table1>(() => aliasTable1)
.Where(p => p.Name == "stringValue1")
.JoinQueryOver(p => p.Table2, () => aliasTable2)
.SelectList(list => list
.Select(() => aliasTable1.Id)
.Select(() => aliasTable1.Time)
.Select(() => aliasTable2.Version)
.SelectSubQuery(
QueryOver.Of<Table1>(() => secondAliasTable1)
.Where(() => aliasTable1.Time < secondAliasTable1.Time)
.WithSubquery.Where(() => secondAliasTable1.Time <
QueryOver.Of<Table1>(() => thirdAliasTable1)
.Where(() => thirdAliasTable1.Name == "stringValue1")
.And(() => thirdAliasTable1.Id == aliasTable1.Id)
.And(() => thirdAliasTable1.Time > aliasTable1.Time)
.SelectList(inner => inner
.Select(() => thirdAliasTable1.Time))
.OrderBy(() => thirdAliasTable1.Time).Asc()
.Take(1)
.As<DateTime>())
.And(() => secondAliasTable1.Name == "stringValue2")
.And(() => secondAliasTable1.Id == aliasTable1.Id)
.SelectList(third => third
.Select(() => secondAliasTable1.Time))
.OrderBy(() => secondAliasTable1.Time).Asc()
.Take(1)))
.OrderBy(() => aliasTable1.Time).Asc()
.List<object[]>();
这会生成如下所示的 SQL:
SELECT this_.Id as y0_,
this_.Time as y1_,
aliastable1_.Version as y2_,
(SELECT TOP (1 /* @p0 */) this_0_.Time as y0_
FROM [Table1] this_0_
WHERE this_.Time < this_0_.Time
and this_0_.Time < (SELECT TOP (1 /* @p1 */) this_0_0_.Time as y0_
FROM [Table1] this_0_0_
WHERE this_0_0_.Name = 'stringValue1' /* @p2 */
and this_0_0_.Id = this_.Id
and this_0_0_.Time > this_.Time
ORDER BY this_0_0_.Time asc)
and this_0_.Name = 'stringValue2' /* @p3 */
and this_0_.Id = this_.Id
ORDER BY this_0_.Time asc) as y3_
FROM [Table1] this_
inner join [Table2] aliastable1_
on this_.Id = aliastable1_.Table1Id
WHERE this_.Name = 'stringValue1' /* @p4 */
ORDER BY this_.Time asc
.List<object[]>()
您也可以投影到您选择的 DTO(使用)TransformUsing
。如果这看起来势不可挡,我强烈建议将每个分离QueryOver
到它自己的变量中,然后从主查询中引用它们。
更新:如果你想使用.TransformUsing
,你需要创建一个null
结果对象并使用.WithAlias()
:
Table1 aliasTable1 = null, secondAliasTable1 = null, thirdAliasTable1 = null;
Table2 aliasTable2 = null;
MyDTO dto = null;
var result = session.QueryOver<Table1>(() => aliasTable1)
.Where(p => p.Name == "stringValue1")
.JoinQueryOver(p => p.Table2, () => aliasTable2)
.SelectList(list => list
.Select(() => aliasTable1.Id).WithAlias(() => dto.Id)
.Select(() => aliasTable1.Time).WithAlias(() => dto.Time)
.Select(() => aliasTable2.Version).WithAlias(() => dto.Version)
.SelectSubQuery(
QueryOver.Of<Table1>(() => secondAliasTable1)
.Where(() => aliasTable1.Time < secondAliasTable1.Time)
.WithSubquery.Where(() => secondAliasTable1.Time <
QueryOver.Of<Table1>(() => thirdAliasTable1)
.Where(() => thirdAliasTable1.Name == "stringValue1")
.And(() => thirdAliasTable1.Id == aliasTable1.Id)
.And(() => thirdAliasTable1.Time > aliasTable1.Time)
.SelectList(inner => inner
.Select(() => thirdAliasTable1.Time))
.OrderBy(() => thirdAliasTable1.Time).Asc()
.Take(1)
.As<DateTime>())
.And(() => secondAliasTable1.Name == "stringValue2")
.And(() => secondAliasTable1.Id == aliasTable1.Id)
.SelectList(third => third
.Select(() => secondAliasTable1.Time))
.OrderBy(() => secondAliasTable1.Time).Asc()
.Take(1)).WithAlias(() => dto.Time2))
.OrderBy(() => aliasTable1.Time).Asc()
.TransformUsing(Transformers.AliasToBean<MyDTO>())
.List<MyDTO>();