任何人都知道如何使用Projections.Conditional生成类似于"case .when.“之类的东西。
以下代码提供了错误的查询:
IProjection isError = Projections.Conditional( Expression.Eq( "event.LogLevel", eLogLevel.Fatal.ToString( ) ), Projections.Constant( 1 ), Projections.Constant( 0 ) );
ICriteria criteria = Session.CreateCriteria( typeof( LogEvent ), "event" )
.Add( Restrictions.Eq( "event.ApplID", "LogEventViewer" ) )
.SetProjection( Projections.ProjectionList( )
.Add( Projections.GroupProperty( "event.ApplID" ) )
.Add( Projections.RowCount( ), "TotalCount" )
.Add( Projections.Sum( isError ), "ErrorCount" )
);所产生的语句是不完整的,参数的顺序是错误的。
exec sp_executesql N'
SELECT this_.strApplID as y0_
, count(distinct this_.lngLogEventID) as y1_
, sum((case when this_.strLogLevel = ? then ? else ? end)) as y2_
, this_.strApplID as y3_
FROM qryLogEvent this_
WHERE this_.strApplID = @p0
GROUP BY this_.strApplID'
,N'@p0 nvarchar(5),@p1 int,@p2 int,@p3 nvarchar(14)'
,@p0=N'Fatal',@p1=1,@p2=0,@p3=N'LogEventViewer' 使用Projections.Conditional的正确方法是什么?
发布于 2009-08-05 07:40:32
更新:这个问题(NH1911)现在在2.1.1.GA版本中被标记为修复了。
它似乎是命名参数和位置参数的结合使用。它似乎确实是一个bug,正如您所得出的结论:
https://nhibernate.jira.com/browse/NH-1911
Projections.Constant使用位置,Restriction.Eq使用命名参数。这扰乱了这里的命令,尽管这个问题应该已经解决了:
https://forum.hibernate.org/viewtopic.php?f=25&t=985944&start=0
https://stackoverflow.com/questions/1222735
复制相似问题