Projections.Conditional - 如何使用它?

Car*_*aro 7 nhibernate icriteria

任何人都知道如何使用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" )
  );
Run Code Online (Sandbox Code Playgroud)

生成的语句不完整,参数的顺序错误.

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' 
Run Code Online (Sandbox Code Playgroud)

使用Projections.Conditional的正确方法是什么?

asg*_*las 2

更新:该问题 (NH1911) 现已在版本 2.1.1.GA 中标记为已修复。尝试检查一下!


似乎是命名参数和位置参数一起使用。这似乎确实是一个错误,您也一定已经得出结论:

https://nhibernate.jira.com/browse/NH-1911

Projections.Constant 使用位置参数,Restriction.Eq 使用命名参数。这会打乱此处所述的顺序,尽管此问题应该已得到解决:

https://forum.hibernate.org/viewtopic.php?f=25&t=985944&start=0