流利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
OR是問題所在 - 我已將C#PredicateBuilder代碼轉換爲VB.NET,並使用Or代替OrElse。我將修改我的原始文章以包含修訂後的PredicateBuilder VB.NET代碼。 –
關於「包含」建議。這是一個基於數據驅動條件即時創建複雜查詢的概念驗證。這個例子沒有公開我將最終包含的額外AND/OR條件。我會盡可能使用「Contains」。謝謝!!! –
另一個編輯:需要更改爲AndAlso –