Fluent NHibernate QueryOver 在子查询上失败
Fluent NHibernate QueryOver fails on subquery
我有一个 table,它有 datetime 字段和 nvarchar 字段。我正在尝试执行一个查询,使用 Fluent Nhibernate QueryOver 仅获取每个 nvarchar 字段的最新行。
Table 在这里:
CREATE TABLE [dbo].[DataItem] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Source] NVARCHAR (50) NULL,
[Target] NVARCHAR (50) NULL,
[SendDateTime] DATETIME NULL,
[Version] INT NULL
);
QueryOver 代码在这里:
DataItem dataItemAlias = null;
var c = QueryOver.Of<DataItem>(() => dataItemAlias);
c.WhereRestrictionOn(x => x.Source).IsInsensitiveLike("A");
DataItem maxSendDateTimeAlias = null;
var subQuery = QueryOver.Of<DataItem>(() => maxSendDateTimeAlias)
.Select(Projections.ProjectionList()
.Add(Projections.Max(() => maxSendDateTimeAlias.SendDateTime))
.Add(Projections.Group(() => maxSendDateTimeAlias.Target)))
.Where(() => dataItemAlias.Source == maxSendDateTimeAlias.Source);
c.WithSubquery.WhereProperty(p => p.SendDateTime).In(subQuery);
var result = c.GetExecutableQueryOver(Session).List<DataItem>();
这是 SQL 查询:
SELECT this_.ID as ID0_0_, this_.Source as Source0_0_, this_.Target as Target0_0_, this_.SendDateTime as SendDate4_0_0_, this_.Version as Version0_0_ FROM [DataItem] this_
WHERE lower(this_.Source) like 'a' and this_.SendDateTime in (SELECT max(this_0_.SendDateTime) as y0_, this_0_.Target as y1_
FROM [DataItem] this_0_ WHERE this_.Source = this_0_.Source GROUP BY this_0_.Target)
如果我删除
this_0_.Target as y1_
来自
SELECT max(this_0_.SendDateTime) as y0_, this_0_.Target as y1_
FROM [DataItem] this_0_...
这是正确的查询,我得到了正确的结果。
这是我得到的错误:
"Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS."
是的,这个错误是真实存在的,这是一个解决方法
var subQuery =
QueryOver.Of<DataItem>()
.Select(
Projections.ProjectionList()
.Add(Projections.SqlGroupProjection("max(SendDateTime) as maxSendDateTimeAlias", "Target",
new string[] { "maxAlias" }, new IType[] { NHibernate.NHibernateUtil.Int32 })));
从
得到答案
我有一个 table,它有 datetime 字段和 nvarchar 字段。我正在尝试执行一个查询,使用 Fluent Nhibernate QueryOver 仅获取每个 nvarchar 字段的最新行。
Table 在这里:
CREATE TABLE [dbo].[DataItem] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Source] NVARCHAR (50) NULL,
[Target] NVARCHAR (50) NULL,
[SendDateTime] DATETIME NULL,
[Version] INT NULL
);
QueryOver 代码在这里:
DataItem dataItemAlias = null;
var c = QueryOver.Of<DataItem>(() => dataItemAlias);
c.WhereRestrictionOn(x => x.Source).IsInsensitiveLike("A");
DataItem maxSendDateTimeAlias = null;
var subQuery = QueryOver.Of<DataItem>(() => maxSendDateTimeAlias)
.Select(Projections.ProjectionList()
.Add(Projections.Max(() => maxSendDateTimeAlias.SendDateTime))
.Add(Projections.Group(() => maxSendDateTimeAlias.Target)))
.Where(() => dataItemAlias.Source == maxSendDateTimeAlias.Source);
c.WithSubquery.WhereProperty(p => p.SendDateTime).In(subQuery);
var result = c.GetExecutableQueryOver(Session).List<DataItem>();
这是 SQL 查询:
SELECT this_.ID as ID0_0_, this_.Source as Source0_0_, this_.Target as Target0_0_, this_.SendDateTime as SendDate4_0_0_, this_.Version as Version0_0_ FROM [DataItem] this_
WHERE lower(this_.Source) like 'a' and this_.SendDateTime in (SELECT max(this_0_.SendDateTime) as y0_, this_0_.Target as y1_
FROM [DataItem] this_0_ WHERE this_.Source = this_0_.Source GROUP BY this_0_.Target)
如果我删除
this_0_.Target as y1_
来自
SELECT max(this_0_.SendDateTime) as y0_, this_0_.Target as y1_ FROM [DataItem] this_0_...
这是正确的查询,我得到了正确的结果。
这是我得到的错误:
"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
是的,这个错误是真实存在的,这是一个解决方法
var subQuery =
QueryOver.Of<DataItem>()
.Select(
Projections.ProjectionList()
.Add(Projections.SqlGroupProjection("max(SendDateTime) as maxSendDateTimeAlias", "Target",
new string[] { "maxAlias" }, new IType[] { NHibernate.NHibernateUtil.Int32 })));
从