7

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 を使用する正しい方法は何ですか?

4

1 に答える 1

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

于 2009-08-05T07:40:32.793 に答える