lambda 函数中未知类型的引用属性

发布于 2024-11-07 21:46:37 字数 4018 浏览 1 评论 0原文

我正在尝试创建一个通用函数来从 Telerik 网格过滤器列表构建 Linq 表达式。我试图远离动态 Linq。因为我不知道要为其构建表达式的类型,所以我尝试在 lambda 函数中使用反射来引用属性,但我的结果从未被过滤。对我的辅助函数的调用以获取属性值从未被调用,尽管它确实击中了“添加”行。将反射放在一条线上(例如下面的“GreaterThan”)不会产生任何影响。这是我的代码:

Public Shared Function buildRadFilter(Of T)(ByVal filterExpression As List(Of GridFilterExpression)) As Expressions.Expression(Of Func(Of T, Boolean))
        Dim returnPred = PredicateBuilder.True(Of T)()

        For Each exp As GridFilterExpression In filterExpression
            Dim iExp = exp
            Dim doDefault = False
            Select Case iExp.FilterFunction
                Case "Contains"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "DoesNotContain"
                    returnPred.And(Function(x) Not DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "StartsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
                Case "EndsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).EndsWith(iExp.FieldValue))
                Case "EqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = iExp.FieldValue)
                Case "NotEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> iExp.FieldValue)
                Case "GreaterThan"
                    'returnPred.And(Function(x) propVal(x, iExp.FieldName) > iExp.FieldValue)
                    returnPred.And(Function(x) x.GetType().GetProperty(iExp.FieldName).GetValue(x, Nothing))
                Case "GreaterThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) >= iExp.FieldValue)
                Case "LessThan"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) < iExp.FieldValue)
                Case "LessThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <= iExp.FieldValue)
                Case "IsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = "")
                Case "NotIsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> "")
                Case "IsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) Is Nothing)
                Case "NotIsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) IsNot Nothing)
                Case "Between"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) >= vals(0) AndAlso propVal(x, iExp.FieldName) <= vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case "NotBetween"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) < vals(0) OrElse propVal(x, iExp.FieldName) > vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case Else
                    doDefault = True
                    Exit Select
            End Select
            If doDefault Then
                returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
            End If
        Next

        Return returnPred
    End Function

    'only works for scalar values
    Public Shared Function propVal(ByRef obj As Object, ByVal name As String) As Object
        Return obj.GetType().GetProperty(name).GetValue(obj, Nothing)
    End Function

提前感谢您提供的任何帮助。

I'm trying to create a generic function to build a Linq expression from a list of Telerik grid filters. I'm trying to stay clear of dynamic Linq. Because I don't know the type that I'm building the expression for, I'm attempting to use reflection in the lambda functions to refer to a property, but my results are never filtered. The call to my helper function to get the property value is never called though it does hit the "Add" lines. Putting the reflection in line (such as the "GreaterThan" below) doesn't make a difference. here is my code:

Public Shared Function buildRadFilter(Of T)(ByVal filterExpression As List(Of GridFilterExpression)) As Expressions.Expression(Of Func(Of T, Boolean))
        Dim returnPred = PredicateBuilder.True(Of T)()

        For Each exp As GridFilterExpression In filterExpression
            Dim iExp = exp
            Dim doDefault = False
            Select Case iExp.FilterFunction
                Case "Contains"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "DoesNotContain"
                    returnPred.And(Function(x) Not DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "StartsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
                Case "EndsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).EndsWith(iExp.FieldValue))
                Case "EqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = iExp.FieldValue)
                Case "NotEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> iExp.FieldValue)
                Case "GreaterThan"
                    'returnPred.And(Function(x) propVal(x, iExp.FieldName) > iExp.FieldValue)
                    returnPred.And(Function(x) x.GetType().GetProperty(iExp.FieldName).GetValue(x, Nothing))
                Case "GreaterThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) >= iExp.FieldValue)
                Case "LessThan"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) < iExp.FieldValue)
                Case "LessThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <= iExp.FieldValue)
                Case "IsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = "")
                Case "NotIsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> "")
                Case "IsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) Is Nothing)
                Case "NotIsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) IsNot Nothing)
                Case "Between"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) >= vals(0) AndAlso propVal(x, iExp.FieldName) <= vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case "NotBetween"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) < vals(0) OrElse propVal(x, iExp.FieldName) > vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case Else
                    doDefault = True
                    Exit Select
            End Select
            If doDefault Then
                returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
            End If
        Next

        Return returnPred
    End Function

    'only works for scalar values
    Public Shared Function propVal(ByRef obj As Object, ByVal name As String) As Object
        Return obj.GetType().GetProperty(name).GetValue(obj, Nothing)
    End Function

Thank you in advance for any help you may be able to offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-11-14 21:46:37

原因是 And 谓词创建了一个您没有保存的新表达式。它不会修改现有的位置。您需要将每个 returnPred.And 行切换为

returnPred = returnPred.And(...

The reason why is the And predicate creates a new expression which you are not saving. It doesn't modify the existing one in place. You need to switch every returnPred.And line to

returnPred = returnPred.And(...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文