这是我的代码:
Public Class Person
Public CountryId as Integer
Public CityId as Integer
End Class
Public MustInherit Class BaseQuery
Protected _IdsForFilter As Integer() = {1,2,3}
Protected Function GetQuery(filter As Func(Of Person, Integer)) As Expression(Of Func(Of Person, Boolean))
Return Function(p) _IdsForFilter.Contains(filter(p))
End Function
End Class
Public Class CountryQuery
Inherits BaseQuery
Public Function GetQueryResult() As IEnumerable(Of Person)
Using session = NhHelper.OpenSession()
Return session.Query(Of Person)().Where(GetQuery(Function(p) p.CountryId))
End Using
End Function
End Class
如您所见,我想将字段Selector for Person(Function(p)p.CountryId)作为lambda函数传递,以使我的搜索通用(在BaseQuery中)。 问题是所有编译,但NHibernate不会返回此查询的任何结果。似乎参数中发送的lambda函数不被解释为lambda函数,而是被解释为委托的标识符。
请问,关于这个问题的任何想法? 换句话说,我应该构建一个表达式树(但我不知道该怎么做)? 提前要求任何帮助
答案 0 :(得分:0)
我终于在LinqKit库的帮助下解决了这个问题,我的函数GetQuery变成了:
Protected Function GetQuery(filter As Func(Of Person, Integer)) As Expression(Of Func(Of Person, Boolean))
Dim expr As As Expression(Of Func(Of Person, Boolean) = Function(p) _IdsForFilter.Contains(filter.Invoke(p))
Return expr.Expand()
End Function
感谢LinqKit,我的外部表达式与内部表达式结合(外部表达式给出的参数p并传递给内部lambda函数)