1

私はレガシーデータを使用しています。これにより、1つの情報が複数の列に分割されることがよくあります。次のSQLクエリを再現しようとしています...

SELECT * FROM SomeTable WHERE concat(DescriptionPart1,DescriptionPart2) LIKE 'TEST'

...NHibernateQueryOverを使用します。それで:

Dim myQuery = Me.Session.QueryOver(Of SomeTable).WhereRestrictionOn( _
    Function(line As SomeTable) line.DescriptionPart1 & line.DescriptionPart2) _
    .IsLike("TEST")

この独自のステートメントは、次の例外に遭遇します。

Variable 'line' of type 'SomeTable' referenced from scope '', but it is not defined

方向性はありますか?私はマジックストリングを避けようとしていますが、常にそれをあきらめています(HQLを使用すると、連結式+関数のようなものが魅力のように機能します)。

4

2 に答える 2

1

少し冗長ですが、機能します

var results = session.QueryOver<SomeTable>()
    .Where(Restrictions.Like(
        Projections.SqlFunction("concat", NHibernateUtil.String, Projections.Property<SomeTable>(x => x.DescriptionPart1), Projections.Property<SomeTable>(x => x.DescriptionPart2)),
        "TEST",
        MatchMode.Anywhere))
    .List();
于 2012-06-11T07:26:59.967 に答える
0

記録のために、私は を使用してこの問題を解決しLinqました。

私の質問の主なポイント (私のせいで、それについては触れていません) は、基本クラスからコードを再利用できる可能性だったので、特定のテーブルの Description 式を抽出して複数の目的に使用したいと考えました。最終的なアイデアは次のように実装されます。

Public MustInherit Class DefaultBusinessLogic(Of Poco)

  Public Overridable ReadOnly Property DescriptionExpression as Expression(Of Func(Of Poco, String))
    Get
       Return Nothing
    End Get
  End Property

  Public Function SearchByDescription(searchArgument as String) as IEnumerable(Of Poco)
     Dim nhSession as ISession = SessionManager.GetSession()

     Dim query = nhSession.Query(Of Poco)

     If Not String.IsNullOrWhitespace(searchArgument) AndAlso
        Me.DescriptionExpression IsNot Nothing Then

        searchArgument = "%" & searchArgument & "%"        

        Dim isLikeMi = ReflectionHelper.GetMethod(Sub() LinqHelpers.IsLike(Nothing, Nothing)) '* See (1)
        Dim isLikeExpression = Expression.Call(isLikeMi, Me.DescriptionExpression.Body, Expression.Constant(searchArgument))
        Dim whereExpression = Expression.Lambda(Of Func(Of Poco, Boolean))(isLikeExpression, Me.DescriptionExpression.Parameters)
        query = query.Where(whereExpression)
     End If

     Return query.ToList()
  End Function

  Public Function GetDescription(pocoRecord as Poco) as String
     If Me.DescriptionExpression Is Nothing Then Return String.Empty

     Return Me.DescriptionExpression.Compile().Invoke(pocoRecord)
  End Function

End Class

Public Class SomeTableBusinessLogic
   Inherits DefaultBusinessLogic(Of SomeTable)

   Public Overrides ReadOnly Property DescriptionExpression as Expression(Of Func(Of Poco, String))
    Get
       Return Function (row as SomeTable) row.DescriptionPart1 & row.DescriptionPart2
    End Get
  End Property

End Class

Public Class SomeTable

   Public Overridable Property Id as Integer
   Public Overridable DescriptionPart1 as String
   Public Overridable DescriptionPart2 as String

End Class

  • (1) 'IsLike' メソッドが NHibernate Linq 拡張機能として実装されました。ここから抜粋
于 2013-03-26T14:51:27.980 に答える