asp.net - lambda 函数中未知类型的引用属性

标签 asp.net vb.net linq telerik

我正在尝试创建一个通用函数来从 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

预先感谢您提供的任何帮助。

最佳答案

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

returnPred = returnPred.And(...

关于asp.net - lambda 函数中未知类型的引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6047816/

相关文章:

c# - 使用 LINQ 在 OrderBy 中自定义排序逻辑

javascript - window.history.back() 在 CSHTML 页面中不起作用

vb.net - 如何使用变量引用控件的名称

.net - 使 Visual Basic 窗体上的元素可拖动

linq - 如何使用 Linq 和 Entity Framework 连接两个可连接对象?

c# - 使用LINQ从逗号分隔的数组中获得独特的值(value)

c# - 为什么@User.IsInRole在_Layout.cshtml中总是返回false

通过 ASP.NET 网站管理员工具获得 Asp.Net 成员(member)资格

c# - 如何设置文本框的值 textmode=date ASP.NET C#

wpf - 如何将DataGrid转换为dataTable