2016-01-22 57 views
1

流利V1.4,NHibernate的v4.0.2.4000,SQL服務器流利,NHibernate的,並且PredicateBuilder

我已經看到了一些帖子,其中謂詞建設者嵌套Lambda表達式不NHibernate的V3評估。難道說v4也有這個問題嗎?

Public Function TestingWorldQuery() As List(Of EmployeeView) 
    Dim EmpView = From q In m_Session.Query(Of EmployeeView)() 
    Dim expr = PredicateBuilder.False(Of EmployeeView)() 
    For Each _opco In {"AGRI", "CORP", "FOODS"} 
     Dim opco As String = _opco 
     expr = expr.Or(Function(x) x.OpCoOrgId = opco) 
    Next 
    EmpView = EmpView.Where(expr) 
    Return EmpView.ToList 
End Function 

無法轉換類型的對象 'NHibernate.Hql.Ast.HqlBitwiseOr' 至 類型 'NHibernate.Hql.Ast.HqlBooleanExpression'


CORRECTION - 在Felipe的建議後,我修改了我的VB.NET PredicateBuilder代碼以使用OrElse。我的代碼現在正在工作。

Imports System.Linq.Expressions 

Public Module PredicateBuilder 
    Public Function [True](Of T)() As Expression(Of Func(Of T, Boolean)) 
     Return Function(f) True 
    End Function 

    Public Function [False](Of T)() As Expression(Of Func(Of T, Boolean)) 
     Return Function(f) False 
    End Function 

    <System.Runtime.CompilerServices.Extension()> _ 
    Public Function [OrElse](Of T)(ByVal expr1 As Expression(Of Func(Of T, Boolean)), ByVal expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean)) 
     Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)()) 
     Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[OrElse](expr1.Body, invokedExpr), expr1.Parameters) 
    End Function 

    <System.Runtime.CompilerServices.Extension()> _ 
    Public Function [AndAlso](Of T)(ByVal expr1 As Expression(Of Func(Of T, Boolean)), ByVal expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean)) 
     Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)()) 
     Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[AndAlso](expr1.Body, invokedExpr), expr1.Parameters) 
    End Function 
End Module 

回答

1

在VB.Net,我們有OrOrElse,你應該更喜歡使用OrElse代替Or因爲Or是bitwised。看看你有的PredicateBuilder,看看你是否使用Expression.OrExpression.OrElse。如有必要,將其更改爲OrElse。它類似於C#|Or||OrElse

您可以使用Contains方法嘗試使用該方法,該方法將生成IN sql語句而不是Or。試試這個:

Public Function TestingWorldQuery() As List(Of EmployeeView) 

    Return m_Session.Query(Of EmployeeView)().Where(Function(x) {"AGRI", "CORP", "FOODS"}.Contains(x.OpCoOrgId)).ToList() 

End Function 

Obs:我沒有測試過它。

+0

OR是問題所在 - 我已將C#PredicateBuilder代碼轉換爲VB.NET,並使用Or代替OrElse。我將修改我的原始文章以包含修訂後的PredicateBuilder VB.NET代碼。 –

+0

關於「包含」建議。這是一個基於數據驅動條件即時創建複雜查詢的概念驗證。這個例子沒有公開我將最終包含的額外AND/OR條件。我會盡可能使用「Contains」。謝謝!!! –

+0

另一個編輯:需要更改爲AndAlso –